chris
chris

Reputation: 16443

Inverse of NSData to UIImage conversion?

In an iPhone app I load a JPG image off a server using a standard HTTP request. The data comes back as NSData, which I can convert into a UIImage using the [UIImage imageWithData:responseData] function. My question is, can I convert the UIImage representation of my image back to the same NSData that it originated from? I know about the UIImageJPEGRepresentation() function, but I tried that with a compressionQuality of 1 and that gave me something different than the original NSData.

Upvotes: 2

Views: 2153

Answers (2)

JeremyP
JeremyP

Reputation: 86651

I think (guess, actually) your issue is probably because a UIImage is not stored in JPEG format internally. Because of the way JPEG compression works, it is unlikely that the conversion back to JPEG could give you the exact same data. Is there any reason why you can't keep the original NSData hanging around?

Upvotes: 3

RyanR
RyanR

Reputation: 7758

You should use UIImagePNGRepresentation(), and send files as PNGs to the device as well. PNG is much more efficient to load and save on the device. This is the standard method of converting an array of bytes (NSData) to/from an image.

If you're stuck with JPEG data, UIImageJPEGRepresentation() is the correct way to get it back to an NSData object. It might not be identical, depending on how Apple implemented that method it could update some of the data while technically still being the same image (would render the same).

Upvotes: 2

Related Questions