【问题标题】:How do I retrieve photos from Photos Library with PHAsset.fetchAssets(withLocalIdentifiers:) in Swift 5.2?如何在 Swift 5.2 中使用 PHAsset.fetchAssets(withLocalIdentifiers:) 从照片库中检索照片?
【发布时间】:2020-04-28 08:19:57
【问题描述】:

我正在尝试使用 asset.localIdentifier 检索照片库中的照片,但 PHAsset.fetchAssets(withLocalIdentifiers:options:) 说:

<extracting data from value failed>

我有一个错误:

"[core] "从守护进程返回的错误:Error Domain=com.apple.accounts Code=7 "(null)""

这是我获取标识符的方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if picker.sourceType == .photoLibrary {
        if let imageURL = info[.referenceURL] as? URL {
            let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
            guard let asset = result.firstObject else {return}
            if let name = selectedProject?.name {
                photo.name = name
                photo.identifier = asset.localIdentifier
                try! realmRepo.realm.write {
                    realmRepo.realm.add(photo)
                }
            }
        }
        self.dismiss(animated: true)
    }
}

这里是我存储标识符的地方:

private var identifiers = [String]()

实际上有 3 个标识符:

(lldb) po identifiers
▿ 3 elements
  - 0 : "106E99A1-4F6A-45A2-B320-B0AD4A8E8473/L0/001"
  - 1 : "106E99A1-4F6A-45A2-B320-B0AD4A8E8473/L0/001"
  - 2 : "B84E8479-475C-4727-A4A4-B77AA9980897/L0/001"

这是我尝试检索它们的方法:

private func getImages() {
    photosToDisplay.removeAll()

    let options = PHFetchOptions()

    options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    let results = PHAsset.fetchAssets(withLocalIdentifiers: self.identifiers, options: options)

    let manager = PHImageManager.default()

    results.enumerateObjects { (thisAsset, _, _) in

        manager.requestImage(for: thisAsset, targetSize: CGSize(width: 80.0, height: 80.0), contentMode: .aspectFit, options: nil, resultHandler: {(thisImage, _) in

            self.photosToDisplay.append(UIImageView(image: thisImage))

        })
    }
    self.galleryCollectionView.reloadData()
}

我已在 info.plist 中正确添加隐私 - 照片库使用说明并通过以下方式验证授权:

private func checkIfUserIsAllowToPickPhotoFromLibrary() {
    let status = PHPhotoLibrary.authorizationStatus()
    if (status == PHAuthorizationStatus.denied || status == PHAuthorizationStatus.notDetermined) {
        PHPhotoLibrary.requestAuthorization({ (newStatus) in
            if (newStatus == PHAuthorizationStatus.authorized) {
                self.pickPhotoFromLibrary()
            } else {
                let alert = self.errorAlert.alert(message: "Invest'Immo a besoin d'avoir accès à votre bibliothèque photo. Sans ça vous ne pourrez pas choisir des photos de votre bibliothèque. S'il vous plait allez dans vos réglages et autorisez l'accès.")
                self.present(alert, animated: true)
            }
        })
    } else if status == PHAuthorizationStatus.authorized {
        pickPhotoFromLibrary()
    }
}

我该如何解决这个问题?

【问题讨论】:

  • 能否在 self.photosToDisplay.append(UIImageView(image: thisImage)) 之前添加一个 print(thisImage) 并发布输出?
  • 你解决过这个问题吗?
  • 你解决了吗? @JohnM
  • 你解决了吗? @乔治

标签: ios swift uiimagepickercontroller photokit phphotolibrary


【解决方案1】:

经过数小时和数天的调试,我找到了与设备设置相关的以下解决方案。

转到设置 -> 搜索您的应用名称并选择它 -> 照片(您将看到“已选择的照片”选项被选中)-> 将选择更改为所有照片,然后重试!

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多