Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 通过向现有UIImage添加阴影来创建新UIImage_Iphone_Objective C_Cocoa Touch_Uiimage_Cgcontext - Fatal编程技术网

Iphone 通过向现有UIImage添加阴影来创建新UIImage

Iphone 通过向现有UIImage添加阴影来创建新UIImage,iphone,objective-c,cocoa-touch,uiimage,cgcontext,Iphone,Objective C,Cocoa Touch,Uiimage,Cgcontext,我看了一下这个问题: 但被接受的答案对我来说并不适用 我要做的是拍摄一个UIImage并向其添加一个阴影,然后返回一个全新的UIImage、阴影和所有内容 这就是我正在尝试的: - (UIImage*)imageWithShadow { CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef shadowContext = CGBitmapContextCreate(NULL, self

我看了一下这个问题:

但被接受的答案对我来说并不适用

我要做的是拍摄一个UIImage并向其添加一个阴影,然后返回一个全新的UIImage、阴影和所有内容

这就是我正在尝试的:

- (UIImage*)imageWithShadow {

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width, self.size.height + 1, CGImageGetBitsPerComponent(self.CGImage), 0, 
                                                 colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadow(shadowContext, CGSizeMake(0, -1), 1);
    CGContextDrawImage(shadowContext, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
    CGContextRelease(shadowContext);

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
    CGImageRelease(shadowedCGImage);

    return shadowedImage;
}
结果是,我得到的图像与之前通过这种方法得到的图像完全相同


我这样做是正确的,还是有明显的遗漏?

您的代码有几个问题:

  • 目标图像太小。确保有足够的地方画阴影
  • 考虑使用
    CGContextSetShadowWithColor
    来定义阴影及其颜色
  • 不要忘记坐标系是翻转的,所以原点是左下角,而不是左上角
通过解决这些问题,应该画出阴影

- (UIImage*)imageWithShadow {
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width + 10, self.size.height + 10, CGImageGetBitsPerComponent(self.CGImage), 0, 
                                             colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadowWithColor(shadowContext, CGSizeMake(5, -5), 5, [UIColor blackColor].CGColor);
    CGContextDrawImage(shadowContext, CGRectMake(0, 10, self.size.width, self.size.height), self.CGImage);

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
    CGContextRelease(shadowContext);

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
    CGImageRelease(shadowedCGImage);

    return shadowedImage;
}

我需要一个方法,可以接受UIImage作为参数,并返回一个稍大的带有阴影的UIImage。这里的帖子对我很有帮助,下面是我的方法。 返回的图像在每一侧都有一个中心5px阴影

+(UIImage*)imageWithShadowForImage:(UIImage *)initialImage {

CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef shadowContext = CGBitmapContextCreate(NULL, initialImage.size.width + 10, initialImage.size.height + 10, CGImageGetBitsPerComponent(initialImage.CGImage), 0, colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);

CGContextSetShadowWithColor(shadowContext, CGSizeMake(0,0), 5, [UIColor blackColor].CGColor);
CGContextDrawImage(shadowContext, CGRectMake(5, 5, initialImage.size.width, initialImage.size.height), initialImage.CGImage);

CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
CGContextRelease(shadowContext);

UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
CGImageRelease(shadowedCGImage);

return shadowedImage;
}

我需要一个更大的阴影。这里是我的答案的变体,它允许自定义阴影大小而没有偏移

    - (UIImage*)imageWithShadow:(UIImage*)originalImage BlurSize:(float)blurSize {

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, originalImage.size.width + (blurSize*2), originalImage.size.height + (blurSize*2), CGImageGetBitsPerComponent(originalImage.CGImage), 0, colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadowWithColor(shadowContext, CGSizeMake(0, 0), blurSize, [UIColor blackColor].CGColor);
    CGContextDrawImage(shadowContext, CGRectMake(blurSize, blurSize, originalImage.size.width, originalImage.size.height), originalImage.CGImage);

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
    CGContextRelease(shadowContext);

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
    CGImageRelease(shadowedCGImage);

    return shadowedImage;
}
像这样使用它:

UIImage *shadowedImage = [self imageWithShadow:myImage BlurSize:30.0f];

要使图像成为本例中的
CGImage
,请尝试

self.shadowImage.CGImage
iOS8/Swift版本(作为UIImage的扩展):

(由Laurent Etiemble和capikaw的答案转换而来)

编辑2016/10/31:转换为Swift 3,并删除所有不必要的内容

用法:

let sourceImage: UIImage(named: "some image")
let shadowImage = sourceImage.addShadow()

GK100在Swift 2/iOS9中的回答

func addShadow(blurSize: CGFloat = 6.0) -> UIImage {

    let data : UnsafeMutablePointer<Void> = nil
    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()!
    let bitmapInfo : CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)

    let myColorValues:[CGFloat] = [0.0, 0.0, 0.0, 0.8]
    let myColor = CGColorCreate(colorSpace, myColorValues)

    let shadowContext : CGContextRef = CGBitmapContextCreate(data, Int(self.size.width + blurSize), Int(self.size.height + blurSize), CGImageGetBitsPerComponent(self.CGImage), 0, colorSpace, bitmapInfo.rawValue)!

    CGContextSetShadowWithColor(shadowContext, CGSize(width: blurSize/2,height: -blurSize/2),  blurSize, myColor)
    CGContextDrawImage(shadowContext, CGRect(x: 0, y: blurSize, width: self.size.width, height: self.size.height), self.CGImage)

    let shadowedCGImage : CGImageRef = CGBitmapContextCreateImage(shadowContext)!
    let shadowedImage : UIImage = UIImage(CGImage: shadowedCGImage)

    return shadowedImage
}
func addShadow(模糊大小:CGFloat=6.0)->UIImage{
let数据:UnsafeMutablePointer=nil
让colorSpace:CGColorSpace=CGColorSpace创建设备GB()!
让bitmapInfo:CGBitmapInfo=CGBitmapInfo(rawValue:CGImageAlphaInfo.PremultipledLast.rawValue)
让myColorValues:[CGFloat]=[0.0,0.0,0.0,0.8]
让myColor=CGColorCreate(颜色空间,mycolorValue)
让shadowContext:CGContextRef=CGBitmapContextCreate(数据,Int(self.size.width+blurSize),Int(self.size.height+blurSize),CGImageGetBitsPerComponent(self.CGImage),0,颜色空间,bitmapInfo.rawValue)!
CGContextSetShadowWithColor(阴影上下文,CGSize(宽度:blurSize/2,高度:-blurSize/2),blurSize,myColor)
CGContextDrawImage(shadowContext,CGRect(x:0,y:blurSize,width:self.size.width,height:self.size.height),self.CGImage)
让shadowedCGImage:CGImageRef=CGBitmapContextCreateImage(shadowContext)!
让shadoweImage:UIImage=UIImage(CGImage:shadowedCGImage)
返回阴影图像
}

我在图像上绘制阴影的方法。它具有自定义阴影参数,并以正确的大小和屏幕比例渲染图像。应该在UIImage扩展中实现。(Swift 3)


在尝试使用这里的其他一些示例时,我注意到它们似乎要么硬编码偏移和模糊,要么在调整输出大小和定位时没有正确考虑它们

以下代码解决了这些问题(它基于Igor Kulagin的原始工作):


完美的我实际上是在高度上加1来绘制阴影,但是其他的东西(设置阴影,翻转坐标系)是非常重要的。这段代码将在哪里实现,以及如何调用它来为图像添加阴影?该方法假设该类可以提供CGImage及其尺寸。您可以更改此方法以获取参数(CGImage和CGSize)。如何将UIImage转换为CGImage?@user403015:使用UIImage的CGImage属性。如果UIImage将显示在UIImageView中,您也可以向视图添加阴影效果。看见
func addShadow(blurSize: CGFloat = 6.0) -> UIImage {

    let data : UnsafeMutablePointer<Void> = nil
    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()!
    let bitmapInfo : CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)

    let myColorValues:[CGFloat] = [0.0, 0.0, 0.0, 0.8]
    let myColor = CGColorCreate(colorSpace, myColorValues)

    let shadowContext : CGContextRef = CGBitmapContextCreate(data, Int(self.size.width + blurSize), Int(self.size.height + blurSize), CGImageGetBitsPerComponent(self.CGImage), 0, colorSpace, bitmapInfo.rawValue)!

    CGContextSetShadowWithColor(shadowContext, CGSize(width: blurSize/2,height: -blurSize/2),  blurSize, myColor)
    CGContextDrawImage(shadowContext, CGRect(x: 0, y: blurSize, width: self.size.width, height: self.size.height), self.CGImage)

    let shadowedCGImage : CGImageRef = CGBitmapContextCreateImage(shadowContext)!
    let shadowedImage : UIImage = UIImage(CGImage: shadowedCGImage)

    return shadowedImage
}
func addingShadow(blur: CGFloat = 1, shadowColor: UIColor = UIColor(white: 0, alpha: 1), offset: CGSize = CGSize(width: 1, height: 1) ) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(CGSize(width: size.width + 2 * blur, height: size.height + 2 * blur), false, 0)
    let context = UIGraphicsGetCurrentContext()!

    context.setShadow(offset: offset, blur: blur, color: shadowColor.cgColor)
    draw(in: CGRect(x: blur - offset.width / 2, y: blur - offset.height / 2, width: size.width, height: size.height))
    let image = UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()

    return image
}
extension UIImage {
    /// Returns a new image with the specified shadow properties.
    /// This will increase the size of the image to fit the shadow and the original image.
    func withShadow(blur: CGFloat = 6, offset: CGSize = .zero, color: UIColor = UIColor(white: 0, alpha: 0.8)) -> UIImage {

        let shadowRect = CGRect(
            x: offset.width - blur,
            y: offset.height - blur,
            width: size.width + blur * 2,
            height: size.height + blur * 2
        )
        
        UIGraphicsBeginImageContextWithOptions(
            CGSize(
                width: max(shadowRect.maxX, size.width) - min(shadowRect.minX, 0),
                height: max(shadowRect.maxY, size.height) - min(shadowRect.minY, 0)
            ),
            false, 0
        )
        
        let context = UIGraphicsGetCurrentContext()!

        context.setShadow(
            offset: offset,
            blur: blur,
            color: color.cgColor
        )
        
        draw(
            in: CGRect(
                x: max(0, -shadowRect.origin.x),
                y: max(0, -shadowRect.origin.y),
                width: size.width,
                height: size.height
            )
        )
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        
        UIGraphicsEndImageContext()
        return image
    }
}