akk
akk

Reputation: 135

How to get image path from photo library and how to retrieve the image from photo library in iphone?

I'm new to iphone. In my small application, how to get image path from photo library. I use this following code to getting images and placed in imageview.

-(IBAction) selectimage
{
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
[UIPicker dismissModalViewControllerAnimated:YES];
imageview.image=[info objectForKey:"UIImagePickerControllerOriginalImage"];
    NSLog("Image Path=%@",imageview.image);
}

Its succefully placed to Imageview. But i cant get its imagepath. I'm using nslog("%@",imageview.image); it shows "Image Path=" in console. How can i get its path and how to retrieve photos from photo library. Any body help me. Thanks in advance.

Upvotes: 10

Views: 26629

Answers (2)

Hiren Panchal
Hiren Panchal

Reputation: 3023

Swift 3.0, You can get imageData from referenceURL by following way

let url = URL(string: "assets-library://asset/asset.JPG?id=1000000007&ext=JPG")
let asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
guard let result = asset.firstObject else {
  return nil
}
let imageManager = PHImageManager.default()
var imageData: Data? = nil
imageManager.requestImageData(for: result, options: nil, resultHandler: { (data, string, orientation, dict) in
  imageData = data
})

Upvotes: 2

Philipp Schlösser
Philipp Schlösser

Reputation: 5219

UIImagePickerControllerOriginalImage returns only the image's data, with no reference to its local storage. I haven't used it myself but I guess what you are looking for is

NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];

At least, it's the only way I know of to retrieve any URL.

To retrieve the image, have a look at this question. But also keep in mind, that you already have the image retrieved with

UIImage* image=(UIImage*)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

so ask yourself if you really need this.

Upvotes: 20

Related Questions