iosobjective-cuiimagepickercontrollernsdatauiimagejpegrepresentation

UIImagePickerController corrupts image


I'm working on an app and I need to get bytes from an UIImage.

For the moment, I use UIImagePickerController, and get the UIImage like this,

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

and then get data from UIImage using this code,

NSData *data = UIImageJPEGRepresentation(image, 1.0);

However, data appears to get corrupted in this case.

On other hand, I have add the jpeg to app's bundle, and tried to convert the image then to data,

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mapa" ofType:@"jpeg"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

Surprisingly this works perfectly.

Here are my questions:

  1. How can I get the bytes of image selected from UIImagePickerController correctly?
  2. Is there a way to add photos to the bundle programmatically?

Thanks!

EDIT:

I have some metadata bytes on the original JPEG ("GEO-Information"). If i store the jpeg on the ipad, that metadata disappear, and I need them :S


Solution

  • Use the below code to store image in document directory;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    [data writeToFile:savedImagePath atomically:NO];   
    

    Here 'data' will be the NSData you have obtained from your image captured.