繁体   English   中英

翠鸟图像在加载前是否已调整大小?

[英]Kingfisher images resizing before loading?

我使用以下命令将图像加载到表格视图中:

        let imageSize: CGFloat = 125.0
        let processor = RoundCornerImageProcessor(cornerRadius: 10)
        let placeholder = UIImage(color: .gray, size: CGSize(width: imageSize, height: imageSize))

        cell.imageView?.kf.indicatorType = .activity
        cell.imageView?.kf.setImage(with: url!, placeholder: placeholder, options: [.transition(.fade(0.2)), .processor(processor)])
        cell.imageView?.layer.masksToBounds = true
        cell.imageView?.clipsToBounds = true
        cell.imageView?.contentMode = .scaleAspectFill

尽管似乎发生的情况是图像大于设置的图像(125.0)一秒钟,然后在加载后正确调整大小。 我假设它采用tableView单元格的大小,即:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }

虽然,我只需要我的图像始终保持为125.0。 有什么帮助吗?

这可能对您有帮助

    cell.imageView?.contentMode = .scaleAspectFit
    class func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage
    {
        let size = image.size

        let widthRatio  = targetSize.width  / image.size.width
        let heightRatio = targetSize.height / image.size.height

        // Figure out what our orientation is, and use that to form the rectangle
        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
        }
        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRect(x: 0, y: 0, width: newSize.width,height: newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        image.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM