Search code examples
iosswiftfirebaseuiimageview

Transparent background turns white


I'm uploading an image with a transparent background to firebase. The background becomes white when I downloaded it somehow. Any idea how to fix this?

Here's my putData function.

let uploadData = imageview.image!.jpegData(compressionQuality: 0.75)
let uploadTask = imagesRef.putData(uploadData!, metadata: nil, completion: { (metadata, error) in
    guard let metadata = metadata else {
        return
    } 

And here is the download function.

URLSession.shared.dataTask(with: NSURL(string: imageURL as! String)! as URL, completionHandler: { (data, response, error) -> Void in
    if error != nil {
        print("ERROR LOADING IMAGES FROM URL: \(String(describing: error))")
        DispatchQueue.main.async {
            imageView.image = UIImage()
        }
        return
    }

    DispatchQueue.main.async {
        if let downloadedImage = UIImage(data: data!) {
            imageCache.setObject(downloadedImage, forKey: NSString(string: imageURL!))
            imageView.image = downloadedImage
        }
    }
}).resume()

Solution

  • I'm uploading an image with a transparent background to firebase. The background becomes white when I downloaded it somehow. Any idea how to fix this?

    You're doing it right, except you're getting getting a JPEG instead of PNG data ;) Remember that JPEG does not support transparent BG.

    Hope it helps!