Ios 当我调用requestImage时,应用程序崩溃
调用此方法时,我的应用程序崩溃,错误线程为1:EXC_BAD_指令:Ios 当我调用requestImage时,应用程序崩溃,ios,swift,photokit,Ios,Swift,Photokit,调用此方法时,我的应用程序崩溃,错误线程为1:EXC_BAD_指令: imageManager.requestImage(for: asset!, targetSize: CGSize(width:100,height:100), contentMode: .aspectFit, options: options, resultHandler: {(image,info) -> Void in}) 视图中的完整代码未加载: override func viewDidLoad()
imageManager.requestImage(for: asset!, targetSize: CGSize(width:100,height:100), contentMode: .aspectFit, options: options, resultHandler: {(image,info) -> Void in})
视图中的完整代码未加载:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self;
collectionView.dataSource = self;
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil);
let asset = fetchResult.firstObject;
let options = PHImageRequestOptions()
options.deliveryMode = .fastFormat
options.isSynchronous = true
let imageManager = PHCachingImageManager();
imageManager.requestImage(for: asset!, targetSize: CGSize(width:100,height:100), contentMode: .aspectFit, options: options, resultHandler: {(image,info) -> Void in
print("Got image")
})
}
有人知道是什么导致了这个错误吗?我认为这与关闭有关,但我不确定是什么错
以下是我尝试调用的方法语法:
func requestImage(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?, resultHandler: (UIImage?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID
谢谢 将@James Zaghini的答案提升到另一个层次 您还需要检查
UIImage
或info是否为nil
guard let asset = asset else { return }
let imageManager = PHCachingImageManager();
imageManager.requestImage(for: asset, targetSize: CGSize(width:100,height:100), contentMode: .aspectFit, options: options, resultHandler: {(image,info) -> Void in
if let img = image{
print("Got Image")
}
if let imgInfo = info{
print("Info")
}
})
好的,答案与代码行无关。我必须先查看然后调试,然后单击查看控制台,这样我就可以看到控制台打印错误消息,告诉我将密钥
NSPhotoLibraryUsageDescription
添加到Info.plist中
当您使用照片框架时,您必须包含钥匙,并说明想要访问相机卷的原因。一旦我把这把钥匙添加到plist中,一切都正常了