Search code examples
iosobjective-cxcodeuiimagensdata

unable to form NSData using UIImage


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

}

Solution

  • enter image description here

    Try this one.

    UIImage *image = [UIImage imageNamed:@"imageName.png"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];