gpt4 book ai didi

ios - 在应用程序中显示图库图片 swift NSRangeException 错误

转载 作者:行者123 更新时间:2023-11-30 11:00:27 24 4
gpt4 key购买 nike

我找到了使用 swift 在应用程序中制作图库的教程,我想在我的应用程序中显示图片,但它不起作用。

这是我的 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 上的线程 1:信号 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)

我不明白为什么!

请帮我解决问题。

谢谢!!

最佳答案

对于第一个编译器错误,它表示您正在返回非可选值的项上使用 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()
}

更新您的代码并分享结果。

关于ios - 在应用程序中显示图库图片 swift NSRangeException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com