【发布时间】:2017-01-31 15:49:41
【问题描述】:
从一种方法检索数据并将其用于另一种方法时,我已经遇到过类似的问题。我尝试使用全局变量来解决问题。它不起作用,但有人建议我改用完成处理程序,它解决了我的问题。
我想现在的问题类似,但我对 iOS 编程很陌生,还没有掌握这个话题。
那么如何在下面的代码中获取图片对象的宽高,并用它来计算缩略图的高度呢?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
// Create a datatask and pass in the request
let datatask = session.dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Get a reference to the imageview element of the cell
let imageView = cell.viewWithTag(1) as! UIImageView
// Create an **IMAGE Object** from the data and assign it into the imageview
imageView.image = UIImage(data: data!)
})
})
datatask.resume()
}
return cell
}
这是我现在计算缩略图高度的方法:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// Get the width of the screen to calculate the height of the thumbnail
return self.view.frame.size.width / hardcodedThumbnailWidth * hardcodedThumbnailHeight
}
如果您能帮我解决这个问题,我将非常感激!
【问题讨论】:
标签: ios image image-processing image-size completionhandler