Create new UIImage by adding shadow to existing UIImage

后端 未结 8 843
半阙折子戏
半阙折子戏 2020-12-08 23:15

I\'ve taken a look at this question: 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 s

相关标签:
8条回答
  • 2020-12-08 23:49

    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.

        - (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];
    
    0 讨论(0)
  • 2020-12-08 23:52

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

    self.shadowImage.CGImage
    
    0 讨论(0)
  • 2020-12-08 23:59

    GK100's answer in 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
    }
    
    0 讨论(0)
  • 2020-12-09 00:01

    Whilst trying to use some of the other examples here, I noticed that they seem to either hard-code the offset & blur, or do not correctly take them into account when sizing and positioning the output.

    The following code solves these issues (it's based on the original work by 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
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:04

    iOS8 / Swift version (made as an extension to 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)

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

    Usage:

    let sourceImage: UIImage(named: "some image")
    let shadowImage = sourceImage.addShadow()
    
    0 讨论(0)
  • 2020-12-09 00:06

    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).

    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
    }
    
    0 讨论(0)
提交回复
热议问题