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:
UIImagePickerController
correctly?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
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.