【问题标题】:App crashes when I call requestImage调用 requestImage 时应用程序崩溃
【发布时间】:2016-08-05 04:05:09
【问题描述】:

调用此方法时,我的应用程序崩溃并出现错误线程 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

谢谢!

【问题讨论】:

    标签: ios swift photokit


    【解决方案1】:

    将@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")
        }
    })
    

    【讨论】:

      【解决方案2】:

      好的,答案与代码行无关。我必须先去查看然后调试然后单击查看控制台,这样我才能看到控制台打印错误消息,告诉我将密钥 NSPhotoLibraryUsageDescription 添加到 Info.plist 中。

      当您使用照片框架时,您必须包含想要访问相机胶卷的原因的密钥。一旦我将此密钥添加到 plist 中,一切正常。

      【讨论】:

        猜你喜欢
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-17
        • 2012-07-29
        相关资源
        最近更新 更多