【问题标题】:How to retrieve info about image object如何检索有关图像对象的信息
【发布时间】: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


    【解决方案1】:

    可以通过像

    这样两种方式获取图片的高度和宽度
    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)
    

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

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2011-01-13
      • 1970-01-01
      相关资源
      最近更新 更多