Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether an UIImage is PNG or JPEG?

Tags:

Currently doing my first steps with iPhone development through MonoTouch, I am playing with an UIImage that I read from the photo library.

What I want to achieve is to get the raw byte array (byte[]) of the image.

I know that there are the UIImageJPEGRepresentation and UIImagePNGRepresentation wrappers in MonoTouch. I also know how to use them. What I don't know is:

How do I decide which of these two functions to call?

I.e. if the original image is a JPEG image, I do not want to get it as an PNG but also as a JPEG, and vice versa.

Is there a way to do this, or am I missing some points on this?

like image 745
Uwe Keim Avatar asked Aug 27 '11 19:08

Uwe Keim


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is UIImage?

An object that manages image data in your app.

How do I find my UIImage path?

Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

How do you declare UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in .


Video Answer


1 Answers

Once you have a UIImage, it is capable of producing either a JPEG or PNG using UIImageJPEGRepresentation or UIImagePNGRepresentation. The format of the original image is only important when the UIImage is being created from it (decides which CFImage provider to load it).

If it's important to your app or algorithm to ensure you save it as the original format, I think you have to maintain that info to use when your writing it. I double checked and couldn't find anything that advertised what format it came from.

Are you going to change the image through UIImageView or does the image stay unchanged in your experience? If it's not changed and you just need the UI to select the image, could you get to file bytes? For example, if you showed the images just to select and then you upload them to a server or something, the UIImage could only be for viewing and selecting and if your data structure remembers which file it came from, you could get the bits back off disk and upload. If your changing the file in the view, then you or the user needs to decide the output (and if jpeg the quality) of the image.

like image 74
bryanmac Avatar answered Dec 04 '22 17:12

bryanmac