How can I get an URL from UIImage? I've got an image from iPhone library and I resize it and I don't want to save the new image but I want to get it's URL.
-
You'll probably have to expand a bit on what you want, Are you asking for a new path for the image, the old path for the image, or something else. The only URL that might be associated with a UIImage is a filesystem url that points to where it exists on disk. – Bergasms Oct 19 '12 at 06:53
4 Answers
You say you don't want to save it, but if you need a URL for a UIImage
, you can easily save a temp copy to get the URL that you need.
For newer swift 5:
// Create a URL in the /tmp directory
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TempImage.png") else {
return
}
let pngData = image.pngData();
do {
try pngData?.write(to: imageURL);
} catch { }
Now you can use the URL to share the image to social media or whatever you need to do with it.
For previous Swift versions:
// Create a URL in the /tmp directory
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TempImage.png") else {
return
}
// save image to URL
do {
try UIImagePNGRepresentation(myImage)?.write(to: imageURL)
} catch { }
See here for more.

- 688
- 7
- 18

- 484,302
- 314
- 1,365
- 1,393
UIImage has no URL or file information. It's just a collection of bytes representing the pixel color data. If you get the UIImage from a UIImagePicker then you can get the URL to the original image in the asset library. But you must get this URL in the image picker delegate method and you must keep track of it separately from the UIImage object.
Edit - based on the OP's comment, this is not the information being requested.
-
2so if I want to get the url then I have to save it somewhere and then I can get the url ? – Shubham Sharma Oct 19 '12 at 06:57
-
Your question is still pretty vague. You are talking about a URL to an image that has been edited or resized. If you are just doing this with a UIImage, there is no URL to this new image. You just have an image in memory. – rmaddy Oct 19 '12 at 06:58
-
A UIImage can be created in memory. In such a case there is no file and no URL. If your UIImage comes from loading a file, then you need to get the URL for the file at the time you load the image. – rmaddy Oct 19 '12 at 14:19
-
1But still can you tell me the alternative solution for getting the url of an image which is in memory ? – Shubham Sharma Oct 20 '12 at 05:33
-
1You just said you got my point. A UIImage object in memory has no URL. There is no URL to get. I'm not sure how else to say it. A UIImage doesn't have a URL unless it has been saved to a file. – rmaddy Oct 20 '12 at 05:37
What you are looking for is the UIImagePickerControllerReferenceURL
parameter provided by the delegate callback in the info dictionary. What you need to do is:
NSURL* imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
And it would look something like this:
assets-library://asset/asset.JPG?id=SOME_GUUID&ext=JPG
You could use this latter to refer back to the same image you got in the UIImage*
in the callback.

- 12,420
- 9
- 82
- 110

- 771
- 6
- 20
You could subclass UIImage and create an override initWithURL: to save the URL to a created property... something like this.
+ (id) initWithURL:(NSURL*)url
{
[super initWithURL:url];
self.url = url;
}

- 879
- 7
- 15