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

[英]Create new UIImage by adding shadow to existing UIImage


I've taken a look at this question: UIImage Shadow Trouble

我看过这个问题:UIImage Shadow Trouble

But the accepted answer didn't work for me.

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

What I'm trying to do is take a UIImage and add a shadow to it, then return a whole new UIImage, shadow and all.

我正在尝试做的是采用UIImage并为其添加阴影,然后返回一个全新的UIImage,阴影和所有。

This is what I'm trying:

这就是我正在尝试的:

- (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;
}

The result is that I get exactly the same image as before I put it through this method.

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

I am doing this the correct way, or is there something obvious I'm missing?

我这样做的方法是正确的,还是有一些明显的东西我不见了?

7 个解决方案

#1


14  

There are several problems with your code:

您的代码有几个问题:

  • The target image is too small. Be sure there enough place to draw the shadow.
  • 目标图像太小。一定要有足够的地方画阴影。

  • Consider using CGContextSetShadowWithColor to define both the shadow and its color.
  • 考虑使用CGContextSetShadowWithColor来定义阴影及其颜色。

  • Don't forget that coordinate system is flipped, so the origin is bottom-left, not top-left.
  • 不要忘记坐标系被翻转,因此原点是左下角,而不是左上角。

By fixing these issues, the shadow should be drawn.

通过解决这些问题,应该绘制阴影。

- (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;
}

#2


5  

I needed a method that would accept an UIImage as a parameter and return a slightly larger UIImage with shadows. The posts here have been very helpful to me so here's my method. The returned image has a centered 5px shadow on each side.

我需要一个接受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;
}

#3


5  

iOS8 / Swift version (made as an extension to UIImage):

iOS8 / Swift版本(作为UIImage的扩展):

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()!)
    }
}

(converted from Laurent Etiemble's & capikaw answers)

(由Laurent Etiemble转换为capikaw答案)

EDIT 2016/10/31: converted to Swift 3, and removed all unnecessary stuff

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

Usage:

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

#4


3  

I needed a bigger shadow for my UIImages. Here is my variant of the answer that allows for a customized shadow size with no offset.

我的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;
}

Use it like:

使用它像:

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

#5


1  

My method that draws a shadow on an image. It has custom shadow parameters and renders image in correct size and screen scale. Should be implemented in UIImage extension. (Swift 3).

我在图像上绘制阴影的方法。它具有自定义阴影参数,并以正确的大小和屏幕比例呈现图像。应该在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
}

#6


0  

To make an image a CGImage as in this case , try

要像在这种情况下使图像成为CGImage,请尝试

self.shadowImage.CGImage

#7


0  

GK100's answer in Swift 2 / iOS9

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
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/05/29/10d4868a5c653e8cc5b8f18af3478a7.html



 
  © 2014-2022 ITdaan.com 联系我们: