convert UIImage to NSData and back to UIImage

39,246

Solution 1

To convert UIImage to NSData, use either UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality) or UIImagePNGRepresentation(UIImage *image)

To convert NSData to UIImage, use [UIImage imageWithData:imageData]

So your example could look like this:

cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
NSData* data = UIImagePNGRepresentation(thumbnail);
ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
[imagesArray addObject:imageDataObject];

References: https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html

Solution 2

Try With this:

Use this for convert UIImage to NSData.

  NSData *imagedata = UIImagePNGRepresentation(thumbnail);

Try this for image data convert back to UIImage

  UIImage *image= [UIImage imageWithData:imagedata];

Solution 3

in Swift version:

    let imageData = image!.pngData()
    let imageFromData = UIImage(data: imageData)

See: https://developer.apple.com/documentation/uikit/uiimage/1624096-pngdata

Share:
39,246
user2769614
Author by

user2769614

Updated on July 17, 2022

Comments

  • user2769614
    user2769614 almost 2 years

    I have to populate table cell from database and images are on device. I've to store that image in imageDataObject, how to convert that UIImage to NSData. Please suggest, I tried many solution but it's not converting back that NSData to UIImage.

        cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
        UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
        NSData* data = ??????????;
        ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] 
        imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
        [imagesArray addObject:imageDataObject];
    
  • János
    János over 9 years
    What if I do not know the image representation wether png or jpg? Is it a problem if I use UIImagePNGRepresentation for jpg too?
  • Hobbyist
    Hobbyist over 8 years
    @János - This will not cause a problem.
  • TomSawyer
    TomSawyer almost 8 years
    How the hell do i know the UIImage is PNG? What if it's JPEG?
  • user4806509
    user4806509 almost 7 years
    Simple, effective! Thanks!
  • Toad
    Toad almost 7 years
    A UIImage is neither jpg or png. it's a raw bitmap. PNG and JPG are a format to transport an image in a smaller format
  • kikeenrique
    kikeenrique almost 2 years
    Simple doc update, as now 'UIImagePNGRepresentation' has been replaced by instance method 'UIImage.pngData()'