0

I am trying to make a simple application using XCode 4.5 which would allow the user to chose any particular image via accessing his photo library, then submit that image for recognition with the help of the Tesseract library.

The problem is that I do not know how to further save the selected user picture, In other words, I can provide the user with an option for going in the picture library and let him chose a picture, but I do not how to then save that selected picture so that it can be further processed.

I hope i made it clear, Appreciate your help.

batman
  • 1,937
  • 2
  • 22
  • 41
Mikeazio
  • 96
  • 1
  • 9
  • where you want to save ? – Manu Dec 27 '12 at 07:34
  • Hi Manhor, What I am looking for is to save image as a UIImage and further use it for processing, It is something like that the user selects the image, and the image is then saved to be further used for processing – Mikeazio Dec 27 '12 at 07:59
  • @user1931486 i have given to answer. But in this case only you have to store image name in NSUserDefault. It is useful to when you want retrive image. – Kalpesh Dec 27 '12 at 08:05
  • Check my answer. The answer given by @Kalpesh doesnt not address the issue of saving images from the image picker controller. – batman Dec 27 '12 at 09:14

3 Answers3

3

Try this, Save image:

NSString *imgName=[@"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

Retrive image:

NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,imageName];
[imageview setImage:[UIImage imageWithContentsOfFile:filePath]];
batman
  • 1,937
  • 2
  • 22
  • 41
Kalpesh
  • 5,336
  • 26
  • 41
  • Thanks for the reply, But I am a bit confused here, The thing is that I will let the user chose the image using the photoLibrary, the next step is that I want to save that Image, let me explain with the help of few lines of code – Mikeazio Dec 27 '12 at 08:13
  • After user choose the image from photoLibrary.. Firstly you have to get document directory path and at this path you have to store image. I have given to code of storing image image in document directory and retriveing image.. Also you have to store imgName in NSUserdefault.. – Kalpesh Dec 27 '12 at 08:20
  • thanks Kalpesh, I will try and let you know, appreciate the help – Mikeazio Dec 27 '12 at 08:25
  • Hi Kalpesh, I wanted some clarification on the use of imagename, I get the undeclared identifier for imagename, Any suggestion for handeling that and Further, It might sound be a bit naive, But i want to know that which is the file which would be the final image, in the sense that once I want to use the final saved image, How would i call it? – Mikeazio Dec 27 '12 at 08:32
  • @user1931486 I am doing some changes in above code .. if it will be working fine . let me know and Please upvote and accept my answer if you like it, thanks. – Kalpesh Dec 27 '12 at 09:14
  • @Kalpesh: your answer doesnt explain how to save the image from the image picker. Please refrain from posting half answers or ignoring details which confuses the OP. – batman Dec 27 '12 at 09:15
0

I am still not clear about what exactly you want. You can always Save Image in or Retrieve Image from Photo Library.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
0

What you need to do is save the image data after selection inside the following delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { You can get the image via the key UIImagePickerControllerOriginalImage in the info dictionary and possibly assign it to an imageview or save it as mentioned by @Kalpesh. Check out apple's documentation for more info. Also check out this good tutorial that shows how to pick photos from the photo album or the camera.

batman
  • 1,937
  • 2
  • 22
  • 41