Reputation: 31
I'm trying to retrieve photos in Photo Library with asset.localIdentifier
but PHAsset.fetchAssets(withLocalIdentifiers:options:)
says:
<extracting data from value failed>
and I have an error:
"[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
Here is how I get identifiers:
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)
}
}
Here is where I store identifiers:
private var identifiers = [String]()
with actually 3 identifiers:
(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"
And here is how I try to retrieve them:
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()
}
I have correctly added Privacy - Photo library usage description in info.plist and verified authorization with:
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()
}
}
How do I fix this?
Upvotes: 3
Views: 2455
Reputation: 1
After Hours & Days of debugging i found the folowing solution which is related to device settings.
Go to Settings -> search for your app name & select it -> photos (you will see "Selected Photos" option selected) -> change selection to All Photos and then retry!
Upvotes: 0