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.
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];
}