I want to set the picture name to "PG-1000" and check if it was created before, if so, replace it. So I need to access the filename properties of the PHAssetChangeRequest.
let myAlbumName = "CustomAlbum"
var albumPlaceholder: PHObjectPlaceholder?
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: myAlbumName)
albumPlaceholder = request.placeholderForCreatedAssetCollection
}, completionHandler: { success, error in
guard let placeholder = albumPlaceholder else {
return
}
let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
guard let album = fetchResult.firstObject else {
return
}
// Create and save
PHPhotoLibrary.shared().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image!)
let assetPlaceholder = assetRequest.placeholderForCreatedAsset
guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) else {
return
}
albumChangeRequest.addAssets([assetPlaceholder] as NSArray)
}, completionHandler: { success, error in
if success {
print("Image saved to album \(myAlbumName)")
} else {
print("Error saving image: \(error?.localizedDescription ?? "Unknown error")")
}
})
})
The code is running fine. It takes the image and saves it with a default name in a custom album, but not with a custom name.