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

时间:2018-08-03 19:40:40

标签: ios swift xcode kingfisher

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

schema.rb

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

        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。有帮助吗?

1 个答案:

答案 0 :(得分: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!
    }