简体   繁体   中英

How to colorize transparent holes in UIImage

I had a PNG image with a hole with transparent pixels inside, i tried so many things to change the "hole color". The solution i think is to make a colored mask with the shape of png image and put it behind the original image, but i have no ideia how to make this.

The lazy way to do this is changing the UIImageView background color, but the UIImageView background square will be there, and i get the same result changing the color of transparent area too. All i want is to change the transparent hole inside a PNG image using Swift

Well if it is a static image, why not just add the filling in Photoshop and use the modified image in your code? If not, you can put another view behind your image view with the filling color that you want as its background color, then use auto layout to set the relative size and position of the background view to the image view.

Try This.It will change transperent part of the image to White Color

func TransperentImageToWhite(image: UIImage) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
    var imageRect: CGRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height)
    var ctx: CGContextRef = UIGraphicsGetCurrentContext()
    // Draw a white background (for white mask)
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0)
    CGContextFillRect(ctx, imageRect)
    // Apply the source image's alpha
    image.drawInRect(imageRect, blendMode: kCGBlendModeNormal, alpha: 1.0)
    var mask: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return mask
}

Urja's answer rewritten as a Swift extension for UIImage that accepts any UIColor as the background color:

extension UIImage {

  func withBackground(color: UIColor) -> UIImage? {
    var image: UIImage?
    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    if let context = UIGraphicsGetCurrentContext() {
      context.setFillColor(color.cgColor)
      context.fill(imageRect)
      draw(in: imageRect, blendMode: .normal, alpha: 1.0)
      image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      return image
    }
    return nil
  }

}

You could subclass UIImageView and then add a another subview behind the main view that holds the image. You could then create a property on your new class called say, HoleColor. This property would then set the background view on your subview.

This way you could arbitrarily change the hole color in code.

Swift 4.1 / Xcode 9.3 / iOS 11.3

func transparentImageBackgroundToWhite(image: UIImage) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
    let imageRect: CGRect = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height)
    let ctx: CGContext = UIGraphicsGetCurrentContext()!
    // Draw a white background (for white mask)
    ctx.setFillColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    ctx.fill(imageRect)
    // Apply the source image's alpha
    image.draw(in: imageRect, blendMode: .normal, alpha: 1.0)
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM