[英]What is the simplest way to insert resized images from Core Data into a UICollectionView?
When I set images from a local array into each collection view cell, scrolling is laggy. 当我将本地数组中的图像设置到每个集合视图单元格中时,滚动就很麻烦。 This is because the full scaled UIImage is being used when the cell will be displayed. 这是因为将在显示单元格时使用完整比例的UIImage。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
let image = images[indexPath.row]
cell.imageView.image = image
return cell
}
In order to try and solve this, I wrote a method that resizes images to their proper size at runtime. 为了尝试解决此问题,我编写了一种在运行时将图像调整为适当大小的方法。
private func resizeImages(images: [UIImage], size: CGSize) -> [UIImage?] {//...}
But resizing all the images in the array in viewDidLoad
took a considerable amount of time. 但是在viewDidLoad
调整数组中所有图像的大小需要花费大量时间。
This is a simple photo album app so I would prefer avoiding any use of activity or loading indicators. 这是一个简单的相册应用程序,因此我希望避免使用任何活动或加载指标。 Given access to image data fetched from Core Data, how can I set the images in a collection view such that it won't lag while scrolling? 可以访问从Core Data获取的图像数据,如何在集合视图中设置图像,以使其在滚动时不会滞后?
Instead of resizing images on runtime you should do this task at time of saving in CoreData
. 除了在运行时调整图像大小外,还应该在保存到CoreData
时执行此任务。 Instead of saving one image, you should store two images. 而不是保存一张图像,您应该存储两张图像。 One with full scale (high resolution) and other thumbnail (resized). 一种具有完整比例(高分辨率),另一种具有缩略图(调整大小)。 Load only thumbnails in your CollectionViewCell
to avoid scroll lagging. 仅将缩略图加载到CollectionViewCell
以避免滚动滞后。
I have used a LazyImageView (originally taken from cocoanetics.com ) for networking which probably work without many changes in your case: 我使用了LazyImageView(最初来自cocoanetics.com )进行联网,在您的情况下可能无需进行很多更改即可工作:
import UIKit
class LazyImageView: UIImageView {
var loading = false
weak var rawImage: UIImage?
deinit {
image = nil
loading = false
}
override func didMoveToSuperview() {
if !loading && image == nil{
loading = true
DispatchQueue.global().async {
self.resizeImage()
}
}
}
func resizeImage() {
let resizedImage: UIImage? = nil
// do the resizing
DispatchQueue.main.async {
self.image = resizedImage
self.loading = false
}
}
}
As you have asked for simple I would consider it done. 正如您所要求的那样,我认为它已经完成。
If you want to spend some more time, you could/should think about caching (ie to the file system's cache folder). 如果您想花更多的时间,则可以/应该考虑进行缓存(即到文件系统的缓存文件夹)。
Depending on the app, maybe you have another nice UX way to give the user a hint what will happen (ie setting correct(?) placeholder color as a background for the image before loading). 根据应用程序的不同,也许您还有另一种不错的UX方法可以向用户提示将要发生的事情(即,在加载之前将正确的(?)占位符颜色设置为图像的背景)。
Considering @Bilal answer to save a correct thumb version sound also well. 考虑@Bilal答案以保存正确的拇指版本声音也很好。 It really depends on your app, ie if the user has a choice to resize the overall image size in the collection view (now or as a future feature). 这实际上取决于您的应用程序,即用户是否可以选择在集合视图(现在或将来的功能)中调整整体图像大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.