简体   繁体   中英

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) {
        print(imageURL)
    }        

    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)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM