Users can download image and video files from a server which are then stored temporarily under the app's Documents path and then copied from there to a custom album in the iOS photo library with either PHAssetChangeRequest.creationRequestForAssetFromImage
or PHAssetChangeRequest.creationRequestForAssetFromVideo
.
This works flawless when only image files OR video files are downloaded but when both images and videos are mixed in the same download batch there are always some files failing arbitrarily with one of these errors:
Error was: The operation couldn’t be completed. (PHPhotosErrorDomain error -1.)
or
Error was: The operation couldn’t be completed. (PHPhotosErrorDomain error 3302.)
This is the responsible code for copying files to the photo library:
public func saveFileToPhotoLibrary(_ url: URL, completion: @escaping (Bool) -> Void)
{
guard let album = _album else
{
completion(false)
return
}
guard let supertype = url.supertype else
{
completion(false)
return
}
if supertype == .image || supertype == .movie
{
DispatchQueue.global(qos: .background).async
{
PHPhotoLibrary.shared().performChanges(
{
guard let assetChangeRequest = supertype == .image
? PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: url)
: PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url) else
{
completion(false)
return
}
guard let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset else
{
completion(false)
return
}
guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) else
{
completion(false)
return
}
albumChangeRequest.addAssets([assetPlaceholder] as NSFastEnumeration)
})
{
saved, error in
if let error = error
{
print("Failed to save image file from \(url.lastPathComponent) to photo gallery. Error was: \(error.localizedDescription)")
DispatchQueue.main.async { completion(false) }
}
}
}
}
}
Does anyone know why the errors happen or how to solve this issue so that no file copies are failing?