如何检索有关图像对象的信息
问题描述:
我已经遇到了从一个方法中检索数据并将其用于另一个方法的类似问题。我试图用全局变量来解决这个问题。它没有工作,但我建议使用完成处理程序,它解决了我的问题。如何检索有关图像对象的信息
我想现在的问题是类似的,但我对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
}
,我会超级感激,如果你帮助我!
答
您可以通过两种方式获得图像的高度和宽度一样
let image = UIImage(named: "image.png")
1. let width1 = image.size.width
let height1 = image.size.height
2. let width2 = CGImageGetWidth(image.CGImage)
let height2 = CGImageGetHeight(image.CGImage)
通过这个,你可以在全球范围保持宽度和高度值,做要紧的计算。