Swift: Save image with file name using PHPhotoLibrary and PHAssetCreationRequest

I  am trying to save an image to the user's photo library using PHPhotoLibrary and set the image file name at the time of saving suing the code below. This is working the first time, but if I then try to save the same image again with a different file name, it saves with the same file name as before. Is there something I need to add to let the system know to save a new version of the image with a new file name? Thank you

PHPhotoLibrary.shared().performChanges ({
let assetType:PHAssetResourceType = .photo
let request:PHAssetCreationRequest = .forAsset()
let createOptions:PHAssetResourceCreationOptions = PHAssetResourceCreationOptions()
        createOptions.originalFilename = "\(fileName)"
        request.addResource(with: assetType, data: image.jpegData(compressionQuality: 1)!, options: createOptions)
        
    }, completionHandler: { success, error in
        if success == true && error == nil {
            print("Success saving image")
        } else {
            print("Error saving image: \(error!.localizedDescription)")
        }
    })

Replies

Can you share more of the surrounding code? Is it possible that fileName or the creatinoOptions are accidentally being shared between the creation requests?

I am also facing similar issue...Did you find solution to this?

Your code is fine, as long as it saves the photo (didn't test your code).

I came across the same issue as you described and after some testing i noticed that the same photo data is only renamed once. I also noticed that if you change anything in the photo data then renaming and saving file works just fine.

I managed to rename file succesfully by setting some random value in EXIF data, just choose any EXIF data key that is most useless to you.

I think iOS has some kind of duplicate recognition handling, because any change in data and renaming works just fine.

I also don't think that it's good way of doing this thing. But couldn't find anything that worked.