0

I have an UIImage and I want to make NSData from it and then save to a filie.

The problem is that I am able to make UIImage but when pass it to make NSData I am getting nil value. I tried both UIImagePNGRepresentation and UIImageJPEGRepresentation

Here is my code with detailed debuging:

UIImage *img  = [self.mImagesArray objectAtIndex:index];
NSData *imageData = UIImagePNGRepresentation(img);  //nil
NSData *imgData2 = UIImageJPEGRepresentation(img, 50); //nil  i tried chaning 50 to 0.5 it also not working
[imageData writeToFile:imagePath atomically:YES];

Here is the image which shows I am getting UIImage but unable to form NSData:

enter image description here

enter image description here

Edit

I am getting image from UIImagePicker and adding it in Array:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

if (!self.mImagesArray)
    self.mImagesArray = [[NSMutableArray alloc] init];

[self.mImagesArray addObject:[info objectForKey:UIImagePickerControllerOriginalImage]];

}
Rahul
  • 5,594
  • 7
  • 38
  • 92

4 Answers4

0

From the docs UIImagePNGRepresentation

Return Value A data object containing the PNG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.


You have UIImage but that doesn't guarantee that it has data, e.g.

UIImage *img = [[UIImage alloc] init];
NSData *data = UIImagePNGRepresentation(img);

In this case you have the non nil UIImage object but it doesn't contain any data thus it will return nil NSData

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

enter image description here

Try this one.

UIImage *image = [UIImage imageNamed:@"imageName.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
vijay
  • 165
  • 1
  • 5
0

This line works for me.

UIImage *img  = [UIImage imageNamed:@"oval.png"];//[self.mImagesArray objectAtIndex:index];
NSData *imageData = UIImagePNGRepresentation(img);

So, you image has problem.

UIImagePNGRepresentation may return nil if image has no CGImageRef or invalid bitmap format

please check

Jamil
  • 2,977
  • 1
  • 13
  • 23
0

Fist of all check your index at which index you are not getting image or getting nil. Because every time of picking image you are allocating memory to array. so first of all allocate array in view Did Load or View Will Appear as per need. and try to debug your code by considering this point.

Hope it will help you !!

Meera
  • 309
  • 2
  • 14
  • HI @meera I have already allocated the array and I am getting Image objects in array as well. But don't know why I am getting this nil value – Rahul Oct 17 '15 at 10:03
  • Actually what you want to do , as per requirement i can suggest u . – Meera Oct 17 '15 at 10:30
  • I have a scenario in which user can upload five images...and then he can add or remove images .. number of images is restricted to five i have to design this scenario – Rahul Oct 17 '15 at 11:26
  • Okay, Do one thing try to store your image in document directory and use this image to show. it will work. to save image refer [link here](http://stackoverflow.com/questions/32606971/i-am-unable-to-save-and-retrieve-image-in-ios/33206125#33206125) – Meera Oct 19 '15 at 03:20