【问题标题】:UIImageView in UITableViewCell low resolution after resizing调整大小后 UITableViewCell 中的 UIImageView 分辨率低
【发布时间】:2016-05-15 15:35:56
【问题描述】:

我真的对自动布局和UITableViewCell 的东西感到沮丧。我有一个带有动态高度单元格的UITableView。细胞内部可以有图像。图像应该适合 UIImageView 的宽度。

图像大于 UIImageView 的大小。所以,下载图片后,我调整了图片大小,然后将其放入UIImageView 中。但是,根据 UIImageView 的宽度调整图像的大小会导致分辨率低,因为 UIScreen 的比例大于 1。所以我尝试在操作后增加图像的预期大小(size= (scale * imageViewWidth, scale * imageViewHeight))。

但是,这一次,单元格的高度变大了 2 倍。我要做的基本上是,在保持 UIImageView 的高度相同的同时,提高分辨率。

class ProgressImageView : UIImageView{
    func setImage(imagePath:String?, placeholder:String, showProgress:Bool, showDetail:Bool){
            if imagePath == nil{
                self.image = UIImage(named: placeholder)
            }else{

                if showProgress{
                    self.setShowActivityIndicatorView(true)
                    self.setIndicatorStyle(.Gray)
                }
                let placeholderImage = UIImage(named: placeholder)
                SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: imagePath!), options: SDWebImageOptions.RefreshCached, progress: nil, completed: { (image, error, _, _, _) in
                    //resize image to fit width
                    if error != nil{
                        self.image = placeholderImage
                    }else {
                        self.setImageFitWidth(image)
                    }
                })
            }
    }
}

extension UIImageView{
    func setImageFitWidth(image: UIImage){
        let w = self.bounds.size.width //* UIScreen.mainScreen().scale
        print ("ImageView size:\(self.bounds.size) scaled width:\(w)")
        self.image = image.convertToWidth(w)
    }
}

extension UIImage{
    func convertToWidth(let width: CGFloat) -> UIImage{
        print ("Old image size:\(self.size)")
        let ratio = self.size.width / width
        let height = self.size.height / ratio
        let size = CGSizeMake(width, height)
        print ("New image size:\(size)")
        UIGraphicsBeginImageContext(size)
        self.drawInRect(CGRectMake(0, 0, size.width, size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

正如您在 setImageFitWidth 方法中看到的,没有 UIScreen.mainScreen().scale 它工作得很好,除了分辨率和比例,UIImageView 的大小翻倍。顺便说一句,我尝试了所有可能的内容模式选项(宽高比匹配、宽高比填充等),并且我正在使用自动布局。

导致低分辨率:

产生高分辨率:

我希望我的屏幕与第一个 ss 中的一样,但图像的分辨率在第二个 ss 中。

【问题讨论】:

  • 我真的不明白问题出在哪里;你能让你的问题更清楚吗?另外,请您提供显示预期结果和实际结果的图像。我也不明白您所说的“提高分辨率”是什么意思。当您放大图像尺寸时,有效分辨率会降低(而不是提高)。
  • @RoboticCat 添加了屏幕截图,并在描述中进行了一些修复。希望这次很清楚。

标签: ios swift uitableview uiimageview autolayout


【解决方案1】:

尝试使用UIGraphicsBeginImageContextWithOptions(size: size, opaque: YES, scale: 0) 绘制你的图像。 比例 0 表示该函数将从设备获得正确的比例因子。

【讨论】:

  • 非常感谢,我假设如果我将图像的大小翻倍,图像视图可以使用宽高比匹配内容模式相应地调整图像,但我错了。感谢您,我现在对 UIImage 和 UIImageView 的工作原理有了更多了解!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
相关资源
最近更新 更多