Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL from UIImage

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.

like image 430
Shubham Sharma Avatar asked Oct 19 '12 06:10

Shubham Sharma


2 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.

like image 166
Suragch Avatar answered Oct 20 '22 08:10

Suragch


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.

like image 35
rmaddy Avatar answered Oct 20 '22 08:10

rmaddy