gpt4 book ai didi

ios - 翠鸟图像调整从 url 加载的图像大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:12 26 4
gpt4 key购买 nike

我有 uitableview,在这个 uitableview 中我有一个单元格。这个单元格有一个 2 个 uiimageview 彼此相邻,宽度相等。我希望这两个 imageviews 占据整个宽度,第一个 imageview 是屏幕宽度的一半,第二个 imageview 是另一半。我正在从 kingfisher 获取图像,如何根据宽度调整从 url 获取的图像的大小。例如,如果图像是 200 - 200,我得到的半宽是 400..我希望我的图像高度调整为 400。如果我得到 300 - 400,我和我得到宽度 150..我希望我的图像高度调整大小到 200。并调整正在处理的单元格的大小。

如何使用 kingfisher 库完成此操作?

最佳答案

我使用 Kingfisher 执行了以下操作。

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)

})

关于ios - 翠鸟图像调整从 url 加载的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42538599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com