当我调用 requestImage 时应用程序崩溃
App crashes when I call requestImage
当调用此方法时,我的应用程序崩溃并出现错误线程 1:EXC_BAD_INSTRUCTION:
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
或信息是否 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 中,一切正常。
当调用此方法时,我的应用程序崩溃并出现错误线程 1:EXC_BAD_INSTRUCTION:
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
或信息是否 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 中,一切正常。