从String URL下载图像

时间:2016-09-19 15:18:04

标签: ios swift

我有以下按钮从集合视图单元格接收数据并共享字符串URL。

但是,我想在共享之前寻找从URL下载实际图像的方法。怎么能在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)

}

1 个答案:

答案 0 :(得分:0)

获取图片网址后,您希望从中创建NSData,然后根据此数据创建UIImage

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