12

我看过这个问题:UIImage Shadow Trouble

但是接受的答案对我不起作用。

我要做的是获取一个 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;
}

结果是我得到的图像与通过这种方法之前完全相同。

我正在以正确的方式执行此操作,还是有明显的我遗漏的东西?

4

8 回答 8

17

您的代码有几个问题:

  • 目标图像太小。确保有足够的位置来绘制阴影。
  • 考虑使用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;
}
于 2010-05-30T16:23:23.750 回答
9

UIImage 的 Swift 扩展:

extension UIImage {
    
    func addShadow(blurSize: CGFloat = 6.0) -> UIImage {
                    
        let shadowColor = UIColor(white:0.0, alpha:0.8).cgColor
        
        let context = CGContext(data: nil,
                                width: Int(self.size.width + blurSize),
                                height: Int(self.size.height + blurSize),
                                bitsPerComponent: self.cgImage!.bitsPerComponent,
                                bytesPerRow: 0,
                                space: CGColorSpaceCreateDeviceRGB(),
                                bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
        
        context.setShadow(offset: CGSize(width: blurSize/2,height: -blurSize/2),
                          blur: blurSize,
                          color: shadowColor)
        context.draw(self.cgImage!,
                     in: CGRect(x: 0, y: blurSize, width: self.size.width, height: self.size.height),
                     byTiling:false)
        
        return UIImage(cgImage: context.makeImage()!)
    }
}

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

用法:

let sourceImage: UIImage(named: "some image")
let shadowImage = sourceImage.addShadow()
于 2014-10-13T15:13:14.270 回答
5

我需要一个接受 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;
}
于 2011-07-28T13:30:10.497 回答
5

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

- (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];
于 2013-01-08T20:43:00.280 回答
4

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

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
}
于 2017-04-15T14:00:14.997 回答
4

在尝试使用此处的其他一些示例时,我注意到它们似乎对偏移和模糊进行了硬编码,或者在调整和定位输出时没有正确考虑它们。

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

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
    }
}
于 2020-08-19T19:50:45.793 回答
0

要在这种情况下制作图像 a CGImage,请尝试

self.shadowImage.CGImage
于 2013-02-05T07:11:29.220 回答
0

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
}
于 2016-04-04T09:05:20.350 回答