UIImagePickerController corrupts image - ios

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:
How can I get the bytes of image selected from UIImagePickerController correctly?
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

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.

Related

Is it possible to save a picture or put a picture in a resource folder that can be retrieve?

I have this picture that I manually added to my project and added called like so.
NSString *path = [[NSBundle mainBundle] pathForResource:#"picture" ofType:#"png"];
But I am wondering if it is possible to take a picture and put it in path for resource that can be retrieve like the code above? My overal goal is to upload my picture to a server with the following code.
[request addFile:path forKey:#"upload"];
Any tips or help will be deeply appreciated.
You can't add a picture (image) to the app bundle, however you can save an image to your apps sandbox. E.g.:
NSData *sourceData = UIImagePNGRepresentation(image);
BOOL result = [sourceData writeToFile:savePath atomically:YES];
where image is a UIImage and savePath is a NSString to the location in which you want to save the image.
If you want to save the image as JPEG (instead of PNG), use UIImageJPEGRepresentation.
You can add the image first to the document directory and get back the url of image like this:
NSData *thumbData = UIImagePNGRepresentation(thumbImg);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imgUrl = [documentsDirectory stringByAppendingFormat:#"/image.png"];
[thumbData writeToFile:imgUrl atomically:NO];
Now image can be getback with the imgUrl like this:
UIImage *img=[UIImage imageWithContentsOfFile:imgUrl];
Hope this helps!
If you are having the NSData of taken picture..
NSData *imageData=UIImageJPEGRepresentation(image);
[request addData:imageData withFileName:#"imagepicture.jpeg" andContentType:#"image/jpeg" forKey:#"upload"];
FileName anything you can give..
If it is PNG image then
NSData *imageData=UIImagePNGRepresentation(image);
[request addData:imageData withFileName:#"imagepicture.png" andContentType:#"image/png" forKey:#"upload"];
Hope it may help you...

iOS : Saving images to app folder and accessing it

I am trying to take an image and place that image to imageView, I am able to do this. But I am not able to save this to device, I want my photo to be saved in my application folder and want to access it from another view.
studentImage is my UIImageView element where I am able to set the image taken from camera.
Here is the code I am working with :
NSString* format = #".png";
NSString* imagename = [NSString stringWithFormat:#"%#%#",nickname,format];
UIImage *img = [self.studentImage image];
NSData *pngData = UIImagePNGRepresentation(img);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:imagename];
[pngData writeToFile:filePath atomically:YES];
This statement is returning null : [pngData writeToFile:filePath atomically:YES];
It would be great, if some one can tell me how to access that image from another view.

iOS-optimized png with transparency

I'm using this excellent OptimizedPNG for downloaded (not Xcode bundled) images, but it appears to turn transparent pixels black. Is there a similar utility (optimized using CgBI format) that works for PNGs that include transparency?
example usage as requested:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:IMAGE_URL]]];
NSData *data = [image optimizedData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"optimized-image.png"];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
Easy answer: Forget "optimized PNGs".
http://imageoptim.com/tweetbot.html
http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/
I am using PNGOUT to compress the images as good as possible and I can
verify that there is no difference in speed.
try setting the UIImageView's backgroundColor property to [UIColor clearColor];

UIImagePickerController delegate method not re-saving/overwriting new pictures taken

I'm using a simple implementation of UIImagePickerController to take a picture and save it to my app directory. I"m running into problems with didFinishPickingMediaWithInfo, where I retrieve the image from the info, convert it into NSData using UIImageJPEGRepresentation, and then save it to a file. Problem is, the NSData object appears never to be overwritten, so the first time I take a picture it saves the picture just fine, but any subsequent pictures are not saved, because when I redisplay the image located at the filePath I'm saving it to, it's always the very first picture I took.
I have NSLog statements right now printing out the hash of the UIImage and of the NSData object I'm saving the image to. The UIImage hash changes each time, but the NSData hash is exactly the same all the time...shouldn't that change too?
My code is below. Note that everything not declared in the delegate method below is a global var that isn't causing me any trouble (at least they don't seem to be).
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fileNameString];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
// imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];
[imageData writeToFile:filePath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
When I get rid of the NSData object entirely and replace all instances of
imageData
with
UIImageJPEGRepresentation(image, 0.5f)
I still have the same problem, namely that the hash of UIImageJPEGRepresentation(image, 0.5f) is always the same, even though the hash of the UIImage it takes as a parameter always changes.
Try this:
NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
imageData = [NSKeyedArchiver archivedDataWithRootObject:imageData];
//imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];
NSError *error= nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&error];
Also take a look at this link
Are you creating the directory? Use this code to create the directory:
NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* docsDir = [dirPaths objectAtIndex:0];
pathString = [docsDir stringByAppendingPathComponent:#"%#",filePath];
NSFileManager* filemgr = [NSFileManager defaultManager];
//Creates Directory
if ([filemgr createDirectoryAtPath:pathString
withIntermediateDirectories:YES
attributes:nil
error: NULL] == YES){

Displaying Saved Images in iOS 5

I have an app that I am saving images to the NSDocumentDirectory. I can see that the path is being set correctly but when I try to retrieve them the NSData is null.
Here are some of my snippets..
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myApp"];
//Create unique filename
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString];
path = [path stringByAppendingPathExtension: #"png"];
[imageData writeToFile:path atomically:NO];
[self showPhoto:path];
Then in my showPhoto method
NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];
If I debug pngData it is 0 bytes but my path show what I think is the correct path?
Any way of debugging if the photo is actually on the phone? Im not sure if it is getting saved or not.
You can do something like this and check if the file exists first and use imageWithContentsOfFile: to retrieve it.
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath];
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists == YES)
{
UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath]
}
If the image is still empty then there is something wrong with your image.
If you running app in simulator you can find application files here: /Users/{user}/Library/Application Support/iPhone Simulator/5.0/Applications
If you debugging on iPhone you can use iExplorer to see files on iPhone.

Resources