作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试在调整图像大小后将图像加载到 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/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!