UIImagePNGRepresentation(UIImage()) returns nil
Asked Answered
R

3

10

Why does UIImagePNGRepresentation(UIImage()) returns nil?

I'm trying to create a UIImage() in my test code just to assert that it was correctly passed around.

My comparison method for two UIImage's uses the UIImagePNGRepresentation(), but for some reason, it is returning nil.

Thank you.

Respectability answered 4/3, 2015 at 4:31 Comment(2)
Are you actually trying to use UIImagePNGRepresentation(UIImage()), or is UIImage() just the placeholder for an already created UIImage?Interlanguage
I have a struct that is pass around and that struct contains a UIImage. I'm setting its property to = UIImage(). Then later I do a comparison between this property and some other UIImage. This comparison is done via UIImagePNGRepresentation(image1) == UIImagePNGRepresentation(image2)Respectability
I
12

UIImagePNGRepresentation() will return nil if the UIImage provided does not contain any data. From the UIKit Documentation:

Return Value

A data object containing the PNG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.

When you initialize a UIImage by simply using UIImage(), it creates a UIImage with no data. Although the image isn't nil, it still has no data. And, because the image has no data, UIImagePNGRepresentation() just returns nil.

To fix this, you would have to use UIImage with data. For example:

var imageName: String = "MyImageName.png"
var image = UIImage(named: imageName)
var rep = UIImagePNGRepresentation(image)

Where imageName is the name of your image, included in your application.

In order to use UIImagePNGRepresentation(image), image must not be nil, and it must also have data.

If you want to check if they have any data, you could use:

if(image == nil || image == UIImage()){
  //image is nil, or has no data
}
else{
  //image has data
}
Interlanguage answered 4/3, 2015 at 4:40 Comment(9)
How do I check if the image has any data or not? Since the UIImagePNGRepresentation will just crash the app if passed a UIImage()...Respectability
That doesn't work, when I try to compare the returned value of UIImagePNGRepresentation the app crashes.Respectability
If you're using this exact code, then it won't work because you don't have a image named MyImageName.png included in your application. That is just an example of what you could do. TL;DR; you have to create a UIImage with some amount of data, somehow, to allow UIImagePNGRepresentation(image) to not return nil. You can't use UIImage() as your image, as it has no data.Interlanguage
But how do I check in my Comparison method if the image passed to UIImagePNGRepresentation has any data? so I can avoid doing UIImagePNGRepresentation(image1) == UIImagePNGRepresentation(image2) (which will crash) if the image1 or image2 have no data.Respectability
I just updated my answer, if you want to check if they have any data, you could just check if image == UIImage(), which essentially checks if the image is equal to an image with no dataInterlanguage
How do I accept 2 answers? Your answer solved one problem for me, but the other guy's answer also solved another problem.Respectability
@RodrigoRuiz I would up vote them, and then accept whichever you think will help others the most in the future.Interlanguage
Your's was the most helpful in terms of understanding (and also helped me fix a big in my Comparison method. I should have added tests for that...), his was the most helpful in the actual tests. Could you edit your answer saying something like "For the test method look at that other answer"?Respectability
Let us continue this discussion in chat.Interlanguage
P
5

The UIImage documentation says

Image objects are immutable, so you cannot change their properties after creation. This means that you generally specify an image’s properties at initialization time or rely on the image’s metadata to provide the property value.

Since you've created the UIImage without providing any image data, the object you've created has no meaning as an image. UIKit and Core Graphics don't appear to allow 0x0 images.

The simplest fix is to create a 1x1 image instead:

UIGraphicsBeginImageContext(CGSizeMake(1, 1))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Primulaceous answered 4/3, 2015 at 4:43 Comment(0)
C
0

I faced the same issue i was converting UIImage to pngData but sometime it returns nil. i fixed it by just creating copy of image

       func getImagePngData(img : UIImage) -> Data {

            let pngData = Data()

            if let hasData = img.pngData(){
                print(hasData)
                pngData = hasData
             }
            else{
                    UIGraphicsBeginImageContext(img.size)
                    img.draw(in: CGRect(x: 0.0, y: 0.0, width: img.width, 
                    height: img.height))
                    let resultImage =
                                   UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
                    print(resultImage.pngData)
                    pngData = resultImage.pngData
            }
         return pngData
        }
Cormac answered 21/7, 2020 at 7:25 Comment(1)
How do you even run this code? pngData is a let, UIImage doesn't have height or width on it...Spavined

© 2022 - 2024 — McMap. All rights reserved.