NSData and UIImage

76,382

Solution 1

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

Solution 2

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];

Solution 3

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.

Share:
76,382
BlueDolphin
Author by

BlueDolphin

Coding is my favorate game, it's fun and addicting.

Updated on September 07, 2020

Comments

  • BlueDolphin
    BlueDolphin over 3 years

    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.