10

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.

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
akk
  • 135
  • 1
  • 1
  • 11

2 Answers2

20

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.

Community
  • 1
  • 1
Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
  • How to retrieve images. i used this code. its shows "assets-library://asset/asset.JPG?id=1000000007&ext=JPG" in console. how to retrieve image. – akk Oct 15 '11 at 11:12
  • Have a look at [this question](http://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone). But also keep in mind, that you already have the image (via UIImagePickerControllerOriginalImage) so do you really need to access it this way? – Philipp Schlösser Oct 15 '11 at 11:22
  • anyway.. i need to place image to imageview and also change imageview when user wants to change photo. anycode u give me. also retrieve images. can u gve the code? – akk Oct 15 '11 at 11:26
  • I just gave you the link on how to retrieve the image. Look, I edited my question again and it answers you question exactly. If you have further problems, please post a new question. Thanks :) – Philipp Schlösser Oct 15 '11 at 11:32
  • One reason to reference the underlying asset is to create a thumbnail of the image using AVAssetImageGenerator. Otherwise, you have to scale the image returned from the original image. – Scott Ahten Apr 19 '16 at 17:30
2

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
})
Hiren Panchal
  • 2,963
  • 1
  • 25
  • 21