gpt4 book ai didi

swift - 在全局线程中调整大小后无法在 uicollectionview 中加载图像

转载 作者:行者123 更新时间:2023-11-30 12:13:27 25 4
gpt4 key购买 nike

我一直在尝试在调整图像大小后将图像加载到 uicollectionview 中。如何使图像在后台线程上调整大小,然后加载到 uicollectionview 中。我尝试了以下代码,但图像未加载到 Collection View 中。

调整大小的函数

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

var newImage = UIImage()
DispatchQueue.global().async {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.draw(in: rect)
newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}


return newImage
}

这是在 cellForItemAt 函数中:

if let imageView = cell.viewWithTag(499) as? UIImageView {
let image = self.cardImages[indexPath.item]
imageView.image = resizeImage(image: image, targetSize: CGSize(width: 100, height: 100))

}

最佳答案

在调整图像大小的方法中,您正在使用线程。因此,您需要在队列括号内使用completionHandler block 。在您的代码中,当您的全局队列异步调用时,您将返回一个空图像。如果您需要代码示例,请告诉我,我将编辑我的答案

========编辑==============

func resizeImage(image: UIImage, targetSize: CGSize, completionHandler: @escaping (_ image: 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

var newImage = UIImage()
DispatchQueue.global().async {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.draw(in: rect)
newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

//now we need to return to the main thread
//NOTE! This is Swift 3 code
DispatchQueue.main.async {
//return the image
completionHandler(newImage)
}
}
}

现在在您想要获取图像的方法中:

if let imageView = cell.viewWithTag(499) as? UIImageView {
let image = self.cardImages[indexPath.item]

resizeImage(image: image, targetSize: CGSize(width: 100, height: 100)) { image in
imageView.image = image
}
}

顺便说一句,我不会在 cellForRow 方法中使用此方法,我更喜欢在自定义单元类中设置它。但取决于你。

关于swift - 在全局线程中调整大小后无法在 uicollectionview 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45729736/

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