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];
Use if-let block with Data to prevent app crash & safe execution of code, as function UIImagePNGRepresentation returns an optional value.
Generic image operations (like png & jpg both):
By using extension:
To convert
UIImage
toNSData
, use eitherUIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)
orUIImagePNGRepresentation(UIImage *image)
To convert
NSData
toUIImage
, use[UIImage imageWithData:imageData]
So your example could look like this:
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
in Swift version:
Try With this:
Use this for convert
UIImage
toNSData
.Try this for image data convert back to
UIImage