我正试图用asset.localIdentifier检索照片库中的照片,但PHAsset.fetchAssets(withLocalIdentifiers:options:)说:
<extracting data from value failed>
我有个错误:
从守护进程返回的
“Domain=com.apple.accounts”错误: 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()
}
}我该怎么解决这个问题?
发布于 2022-03-16 18:52:26
经过几个小时和几天的调试,我找到了与设备设置相关的折叠解决方案。
转到设置->搜索您的应用程序名称&选择它->照片(您将看到“选定的照片”选项被选中) ->更改选择到所有照片,然后重试!
https://stackoverflow.com/questions/61475695
复制相似问题