如何检索有关图像对象的信息

时间:2016-09-22 22:13:00

标签: ios image image-processing image-size completionhandler

我从一种方法检索数据并将其用于另一种方法时遇到了类似的问题。我试图使用全局变量解决问题。它没有用,但我建议使用完成处理程序,它解决了我的问题。

我想现在的问题很相似,但我对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
    }

如果你帮助我,我会非常感激!

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式获取图像的高度和宽度,例如

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)

通过这种方式,您可以全局维护宽度和高度值,并进行必要的计算。

相关问题