Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSData and UIImage

I am trying to load UIImage object from NSData, and the sample code was to NSImage, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImage loading NSData issue.

like image 270
BlueDolphin Avatar asked Nov 02 '08 21:11

BlueDolphin


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 .

How do you convert CIImage to UIImage?

This is most compatibile way of doing it: UIImage* u =[UIImage imageNamed:@"image. png"]; CIImage* ciimage = [[CIImage alloc] initWithCGImage:u. CGImage];


2 Answers

I didn't try UIImageJPEGRepresentation() before, but UIImagePNGRepresentation works fine for me, and conversion between NSData and UIImage is dead simple:

NSData *imageData = UIImagePNGRepresentation(image); UIImage *image=[UIImage imageWithData:imageData]; 
like image 169
b123400 Avatar answered Oct 04 '22 06:10

b123400


UIImage has an -initWithData: method. From the docs: "The data in the data parameter must be formatted to match the file format of one of the system’s supported image types."

like image 43
Noah Witherspoon Avatar answered Oct 04 '22 06:10

Noah Witherspoon