Loading/Downloading image from URL on Swift

前端 未结 30 3158
感动是毒
感动是毒 2020-11-21 05:39

I\'d like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I\'ve a compilation error:

30条回答
  •  没有蜡笔的小新
    2020-11-21 05:48

    If you are looking for a very very simple implementation. (This worked for me in Swift 2)

     let imageURL = NSURL(string: "https://farm2.staticflickr.com/1591/26078338233_d1466b7da2_m.jpg")
     let imagedData = NSData(contentsOfURL: imageURL!)!
     imageView?.image = UIImage(data: imagedData)
    

    I implemented within a tableview with a custom cell that has only a image

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    
            let cell = tableView.dequeueReusableCellWithIdentifier("theCell", forIndexPath: indexPath) as! customTableViewCell
    
            let imageURL = NSURL(string: "https://farm2.staticflickr.com/1591/26078338233_d1466b7da2_m.jpg")
    
            let imagedData = NSData(contentsOfURL: imageURL!)!
    
            cell.imageView?.image = UIImage(data: imagedData)
    
            return cell
    
        }
    

提交回复
热议问题