简体   繁体   English

如何在UIImage中着色透明孔

[英]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". 我有一个PNG图片,里面有一个透明像素的孔,我尝试了很多事情来更改“孔颜色”。 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. 我认为解决方案是用png图像的形状制作一个彩色的蒙版,并将其放在原始图像的后面,但是我不知道如何做到这一点。

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. 懒惰的方法是更改UIImageView背景颜色,但是UIImageView背景正方形将在那里,并且更改透明区域的颜色也得到相同的结果。 All i want is to change the transparent hole inside a PNG image using Swift 我想要的是使用Swift更改PNG图像内的透明孔

Well if it is a static image, why not just add the filling in Photoshop and use the modified image in your code? 好吧,如果它是静态图像,为什么不只在Photoshop中添加填充并在代码中使用修改后的图像呢? 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: Urja的答案被重写为UIImage的Swift扩展,该扩展接受任何UIColor作为背景色:

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. 您可以子类化UIImageView,然后在保存图像的主视图后面添加另一个子视图。 You could then create a property on your new class called say, HoleColor. 然后,您可以在新类上创建一个属性,例如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 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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