1

I want to write a text string on a fully transparent image (alpha 0 everywhere) but it doesn't work. The background of the image turns to be white if alpha of background image is 0. Here are the approaches I tried:

extension UIColor {
  func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
      return UIGraphicsImageRenderer(size: size).image { rendererContext in
          self.setFill()
          rendererContext.fill(CGRect(origin: .zero, size: size))
      }
  }
}


 func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
    let textColor = UIColor.blue
    let textFont = UIFont(name: "Helvetica Bold", size: 40)!

    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale)

    let textFontAttributes = [
        NSAttributedString.Key.font: textFont,
        NSAttributedString.Key.foregroundColor: textColor,
        ] as [NSAttributedString.Key : Any]
    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))

    let rect = CGRect(origin: point, size: image.size)
    text.draw(in: rect, withAttributes: textFontAttributes)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

And then:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let inImage = UIColor.black.withAlphaComponent(0).image(CGSize(width: 800, height: 800))
     //Even tried inImage = UIImage(named: "Transparent") where Transparent.png is fully transparent image! // 

    let image = textToImage(drawText: "Test String", inImage: inImage, atPoint: CGPoint(x: 0, y: 0))
    
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

No matter what I do, the background is white.

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • 1
    For me your code work just fine, maybe you are saving to jpeg instead of png, which support alpha channel? – Reinier Melian Aug 26 '20 at 08:12
  • Ok it turns out UIImageWriteToSavedPhotosAlbum doesn't saves images with full alpha to photo library because by default it is in jpeg. Tried on UIImageView and works! – Deepak Sharma Aug 26 '20 at 08:14

2 Answers2

1

enter image description here

I've tested Your code in playgroung, and it works as it should

So The reason is Apple Gallery representations of alpha channels. Looks like it's not supported

Kstin
  • 659
  • 1
  • 4
  • 18
  • Ok so followed this code to save png to Photo Library and it works - https://stackoverflow.com/questions/1489250/uiimagewritetosavedphotosalbum-save-as-png-with-transparency – Deepak Sharma Aug 26 '20 at 08:19
1

Try converting to png

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let inImage = UIColor.black.withAlphaComponent(0).image(CGSize(width: 800, height: 800))
     //Even tried inImage = UIImage(named: "Transparent") where Transparent.png is fully transparent image! //

    let image = textToImage(drawText: "Test String", inImage: inImage, atPoint: CGPoint(x: 0, y: 0))

    self.image.image = image


    if let pngdata = image.pngData() {
        if let newImage = UIImage(data: pngdata) {
            UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
        }
    }
}
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55