在应用程式快速NSRangeException错误中显示图库图片

时间:2018-11-23 16:33:51

标签: ios swift

我找到了一个教程,他们迅速在应用程序中创建了图库,我想在m'y应用程序中显示图片,但这没用。

有我的CollectionController:

import UIKit
import Photos

class CollectionController: UICollectionViewController {

    var imageArray = [UIImage]()

    override func viewDidLoad() {
        grabPhotos()
    }

    func grabPhotos(){

        let imgManager = PHImageManager.default()
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true

        requestOptions.deliveryMode = .opportunistic

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

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

            if fetchResult.count > 0 {

                for i in 0...fetchResult.count{

                    imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
                        image, error in
                        self.imageArray.append(image!)
                    }
                }
            }
            else{
                print("You don't have photos");
                self.collectionView.reloadData()
            }
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

        let imageView = cell.viewWithTag(1) as! UIImageView

        imageView.image = imageArray[indexPath.row]

        return cell

    }

}

myCell.swift:

import UIKit

class myCell: UICollectionViewCell {

    @IBOutlet weak var myImageView: UIImageView!
}

此代码返回以下错误,并且我的屏幕白屏,并且AppDelegate上的Thread 1: signal SIGABRT

2018-11-23 17:14:56.499653+0100 My app[1584:453752] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x28276c900: index (551) beyond bounds (551)'
*** First throw call stack:
(0x212243ef8 0x211411a40 0x212148ac4 0x220fcd434 0x220fee08c 0x1001db530 0x1001db0f4 
    0x1001db128 0x23f189e4c 0x23f18a27c 0x23f261e90 0x23f262438 0x23f27342c 0x23eabfe10 
    0x23eac57ac 0x23f2ecdb8 0x23f2e9364 0x23f2eca34 0x23f2ed3d4 0x23f2ac5fc 0x23f2ac2a8
    0x23f2ef844 0x23f2f0334 0x23f2ef6fc 0x23f2e8a10 0x23eac3ca4 0x23eaf545c 0x214c73890
    0x214c7e658 0x214c7dd50 0x1011c0de4 0x1011c4a2c 0x214cb2640 0x214cb22cc 0x214cb28e8
    0x2121d25b8 0x2121d2538 0x2121d1e1c 0x2121ccce8 0x2121cc5b8 0x214440584 0x23eac7558
    0x1001d62e8 0x211c8cb94)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我不明白为什么!

请帮助我解决问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

对于第一个编译器错误,这意味着您正在对返回非可选值的项目使用if let条件。

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

    if fetchResult.count > 0 {

        for i in 0...fetchResult.count{

            imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions) {
                image, error in
                self.imageArray.append(image!)

            }

        }

    }
    else{
        print("You don't have photos");
        self.collectionView.reloadData()
    }

更新代码并共享结果。