簡體   English   中英

如何使用更少的內存在 collectionView 中顯示照片庫

[英]How to display photo library in collectionView using less memory

我面臨的問題是,人們說在 collectionView 中加載照片庫時應用程序崩潰。 大多數時候,他們有超過 2-3 千張照片。 對我來說它工作正常。 (我的圖庫中只有不到 1000 張照片)。

問題:也許有什么方法可以在 collectionView 中顯示更“智能”的圖像並使用更少的內存?

PS也許當用戶在iCloud中擁有他的所有照片時也會導致崩潰? 因為直到現在我認為該應用程序在將其加載到單元格時不會下載照片。 也許有人可以證明或反對這一事實。

這是我的功能:

func grabPhotos(){

    let imgManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = false
    requestOptions.deliveryMode = .opportunistic // Quality of images
    requestOptions.isNetworkAccessAllowed = true

    requestOptions.isSynchronous = true
    requestOptions.progressHandler = {  (progress, error, stop, info) in
        print("progress: \(progress)")
    }

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

        if fetchResult.count > 0 {
            for i in 0..<fetchResult.count {
                imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFill, options: requestOptions, resultHandler: { image, error in
                    self.imageArray.append(image!)
                    self.kolekcija.reloadData()
                })
            }
        }
        else {
            print("You got no photos")
            kolekcija.reloadData()
        }
    }

}

問題是這一行:

self.imageArray.append(image!)

切勿制作圖像陣列。 圖像非常大,您將耗盡內存。

這是一個集合視圖。 您只需在itemForRowAt:按需提供圖像,然后提供的是圖像的較小版本(所謂的縮略圖 ),是顯示尺寸的最小版本。 因此,在任何時候,只有足夠的縮略圖可以填充可見屏幕; 這樣就不會占用太多內存,而且當圖像從屏幕上滾動時,運行時還可以釋放圖像內存。

那就是PHCachingImageManager的目的。 在示例代碼中仔細查看Apple如何處理此問題:

https://developer.apple.com/library/archive/samplecode/UsingPhotosFramework/Listings/Shared_AssetGridViewController_swift.html

在該示例中, 任何地方都沒有圖像陣列。

在 Swift 中工作 5. 獲取照片資產,但在 cellForItem 迭代之前不要加載照片。 快樂編碼。

    var personalImages: PHFetchResult<AnyObject>!
            
    func getPersonalPhotos() {
         let fetchOptions = PHFetchOptions()
         fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
         self.personalImages = (PHAsset.fetchAssets(with: .image, options: fetchOptions) as AnyObject?) as! PHFetchResult<AnyObject>? 
            
         photoLibraryCollectionView.reloadData()
    }
        
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
         return personalImages.count
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "insertCellIdentifierHere", for: indexPath) as! InsertACollectionViewCellHere
    
    let asset: PHAsset = self.personalImages[indexPath.item] as! PHAsset
    let itemWidth = photoLibraryCollectionView.frame.size.width / 2
    let itemSize = CGSize(width: itemWidth, height: itemWidth / 2) // adjust to your preferences
    
    PHImageManager.default().requestImage(for: asset, targetSize: itemSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
        if result != nil {
            
            cell.photoImageView.image = result
        }
    })
    
    cell.layoutIfNeeded()
    cell.updateConstraintsIfNeeded()
    return cell
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM