user3591436 user3591436 - 1 month ago 9
iOS Question

Download image from String URL

I have the following button that receives data from a collection view cell and shares a String URL.

However I'm looking for a means to download the actual image from the URL before sharing. How can this be done in Swift?

//get buttonViewShareAction and share image
@IBAction func buttonViewShareAction(sender: UIButton) {
print("buttonViewShareAction tapped")
let face = self.faces[sender.tag]

if let imageURL = NSURL(string:face.image) {

let shareItems:Array = [face.image]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)



After getting the image URL, you want to create an NSData out of it then create a UIImage from this data.

Swift 3.0

let imageData: Data = try! Data(contentsOf: URL(string: imageURL)!)
let profileImage = UIImage(data: imageData)!

Swift 2

let imageData: NSData = NSData(contentsOfURL: NSURL(string: imageURL)!)!
let profileImage = UIImage(data: imageData)!