views:

5308

answers:

5
+1  Q: 

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.

Thanks.

+3  A: 

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."

Noah Witherspoon
+1  A: 

Yes, that's what I am using. The NSData has address, while after call UIImaeg -initWithData:theData, the point become 0. I am not sure where to see the error message.

BlueDolphin
Remember this isn't a thread. Please edit your question to make it clear what you are doing.
Airsource Ltd
+1  A: 

theData should be a NSData object which already contains the data. You need to do the file loading/downloading to the NSData object before it is used. You can inspect it by using NSLog on theData and see if it contains the valid data.

leonho
A: 

I see. Thanks.

BlueDolphin
This is not a blog - This should be in comments - not as an answer. You should delete this answer.
sugar
A: 

I'm having a similar problem. I convert the image to NSData like so...

NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);

then I write it like so...

[topImageData writeToFile:topPathToFile atomically:NO];

then when I try to retrieve it I've tried ...

topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];

which returns no image, so then i tried...

    NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];

topSubView.image = [[UIImage alloc] initWithData:data];

When I'm debugging, I can see that data contains the correct number of bytes, but I'm confused as to why my image is not being created. Am I missing a step?

Greatly appreciate the help!

Patrick