views:

1130

answers:

1

UIImagePickerView Controller returns NSData of image,

My requirement is to store the path of an image as a varchar data type.

After selecting an image from UIImagePickerView, how can I obtain the complete path of the selected image of Photo Gallery of iPhone?

My application won't have to worry about storing images within my application. The images are already stored on the iPhone's photo Gallery.

Thanks in advance.

+2  A: 

This is not possible. The UIImagePickerController will give you a UIImage*, and you have to convert it into NSData yourself, and store it in your local sandbox. There is no SDK-approved way to access the images in the user's photo gallery (for security reasons).

Assuming you're using SDK 3.0, here is a function on the return of the picker that will save it to the disk, in your application's documents directory (this should work, though I may have a small mistake somewhere):

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 // Dismiss the picker
 [[picker parentViewController] dismissModalViewControllerAnimated:YES];

 // Get the image from the result
 UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

 // Get the data for the image as a PNG
 NSData* imageData = UIImagePNGRepresentation(image);

 // Give a name to the file
 NSString* imageName = "MyImage.png";

 // Now, we have to find the documents directory so we can save it
 // Note that you might want to save it elsewhere, like the cache directory,
 // or something similar.
 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString* documentsDirectory = [paths objectAtIndex:0];

 // Now we get the full path to the file
 NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

 // and then we write it out
 [imageData writeToFile:fullPathToFile atomically:NO];

 return;
}
Itay
sugar
Yes. Unfortunately, Apple does not provide a way to get to the original image. You can do this unofficially by looking through the user's photo gallery manually (it's somewhere in /private), and trying to find an image which matches (costly).Regardless, if you store the image as an NSData on disk, even a huge image (several megapixels) shouldn't take up too much space. So in reality, this is rarely an issue.
Itay
Would you help me, How to store that UIImage to my Application?
sugar
edited my answer to have this info.
Itay