如何在UIImage中着色透明孔

时间:2021-02-08 00:05:48

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.

我有一个带有透明像素的孔的PNG图像,我尝试了很多东西来改变“洞颜色”。我认为解决方案是制作一个带有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. All i want is to change the transparent hole inside a PNG image using Swift

懒惰的方法是更改​​UIImageView背景颜色,但UIImageView背景方块将在那里,我也得到相同的结果改变透明区域的颜色。我想要的是使用Swift更改PNG图像内的透明孔

5 个解决方案

#1


3  

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.

好吧,如果它是静态图像,为什么不在Photoshop中添加填充并在代码中使用修改后的图像?如果没有,您可以在图像视图后面放置另一个视图,并使用您想要的填充颜色作为背景颜色,然后使用自动布局将背景视图的相对大小和位置设置为图像视图。

#2


3  

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

试试This.It会将图像的transperent部分改为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
}

#3


0  

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.

您可以继承UIImageView,然后在保存图像的主视图后面添加另一个子视图。然后,您可以在新类上创建名为say,HoleColor的属性。然后,此属性将在子视图上设置背景视图。

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

这样你就可以随意改变代码中的孔颜色。

#4


0  

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
  }

}

#5


0  

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
}

#1


3  

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.

好吧,如果它是静态图像,为什么不在Photoshop中添加填充并在代码中使用修改后的图像?如果没有,您可以在图像视图后面放置另一个视图,并使用您想要的填充颜色作为背景颜色,然后使用自动布局将背景视图的相对大小和位置设置为图像视图。

#2


3  

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

试试This.It会将图像的transperent部分改为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
}

#3


0  

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.

您可以继承UIImageView,然后在保存图像的主视图后面添加另一个子视图。然后,您可以在新类上创建名为say,HoleColor的属性。然后,此属性将在子视图上设置背景视图。

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

这样你就可以随意改变代码中的孔颜色。

#4


0  

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
  }

}

#5


0  

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
}