使用翠鳥我做了以下。
let url = URL(string: urlImage)
cell.imageView?.kf.setImage(with: url,
placeholder: nil,
options: [.transition(ImageTransition.fade(1))],
progressBlock: { receivedSize, totalSize in
print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { image, error, cacheType, imageURL in
print("\(indexPath.row + 1): Finished")
print(image?.size)
cell.imageView?.image = self.resizeImage(image: image!, newWidth: 40.0)
})
您CAND修改resizeImage功能,以改變你的圖像大小,在我的情況我犯了一個方形的圖像。
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth/image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
,或者您可以通過用戶KingfisherManager
KingfisherManager.shared.retrieveImage(with: url!, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
cell.imageView?.image = ImageUtils.resizeImage(image: image!, newWidth: 40.0)
})