キングフィッシャーを使用して、以下のことを行いました。
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)
})
イメージサイズを変更するために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)
})