NSData and UIImage

IosIphoneCocoa TouchUiimageNsdata

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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 2 - Ios

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 3 - Ios

Try this to convert an image to NSdata:

UIImage *img = [UIImage imageNamed:@"image.png"];
NSData *data1 = UIImagePNGRepresentation(img);

Solution 4 - Ios

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.

Solution 5 - Ios

For safe execution of code, use if-let block with Data, as function UIImagePNGRepresentation returns, optional value.

if let img = UIImage(named: "Hello.png") {
    if let data:Data = UIImagePNGRepresentation(img) {
       // Handle operations with data here...         
    }
}

> Note: Data is Swift 3 class. Use Data instead of NSData with > Swift 3

Generic image operations (like png & jpg both):

if let img = UIImage(named: "Hello.png") {
        if let data:Data = UIImagePNGRepresentation(img) {
               handleOperationWithData(data: data)     
        } else if let data:Data = UIImageJPEGRepresentation(img, 1.0) {
               handleOperationWithData(data: data)     
        }
}

*******
func handleOperationWithData(data: Data) {
     // Handle operations with data here...
     if let image = UIImage(data: data) {
        // Use image...
     }
}

By using extension:

extension UIImage {
    
    var pngRepresentationData: Data? {
        return UIImagePNGRepresentation(img)
    }

    var jpegRepresentationData: Data? {
        return UIImageJPEGRepresentation(self, 1.0)
    }
}

*******
if let img = UIImage(named: "Hello.png") {
      if let data = img.pngRepresentationData {
              handleOperationWithData(data: data)     
      } else if let data = jpegRepresentationData {
              handleOperationWithData(data: data)     
     }
}
    
*******
func handleOperationWithData(data: Data) {
     // Handle operations with data here...
     if let image = UIImage(data: data) {
        // Use image...
     }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlueDolphinView Question on Stackoverflow
Solution 1 - Iosb123400View Answer on Stackoverflow
Solution 2 - IosNoah WitherspoonView Answer on Stackoverflow
Solution 3 - Iosuser810340View Answer on Stackoverflow
Solution 4 - IosleonhoView Answer on Stackoverflow
Solution 5 - IosKrunalView Answer on Stackoverflow