简体   繁体   English

将来自Core Data的调整大小的图像插入UICollectionView的最简单方法是什么?

[英]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.

相关问题 从核心数据加载UICollectionView - load UICollectionView from core data 将所选的“根”核心数据实体及其所有关系一起编码的最简单方法是什么? - What's the simplest way to encode a chosen 'root' Core Data entity together with all of its relationships? 在不丢失数据的情况下更新Data Core中的实体的最简单方法 - Simplest way to update Entities in Data Core without losing data 在UICollectionView中重新加载数据的正确方法是什么? - What is the correct way to reload data in a UICollectionView? 如何以编程方式在uicollectionview中插入图像? - How to insert images in uicollectionview programmatically? UICollectionView和核心数据 - UICollectionView and core data 从 Bundle.main 加载/清除数据到 UICollectionView 的理想方法是什么? - What's an ideal way to load/clear data to UICollectionView from Bundle.main? 从应用程序将设备连接到iPad的最简单方法是什么? - What is the simplest way to connect a device to an iPad from an application? 从iOS设备流向Wowza服务器的最简单方法是什么? - What is the simplest way to stream from an iOS device to a Wowza server? 图像数组不是从数据加载到UICollectionView中,而是从简单数组中加载吗? - Array of images not loading into UICollectionView from data but do from simple array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM