Maddie Maddie - 2 years ago 150
Swift Question

Choosing a picture causes resave to camera roll

I have a program in which the user chooses a photo to put on the screen and the code puts it into a custom album automatically. But whenever they choose a picture, it resaves it to the camera roll, creating duplicates. How do I make it stop doing this?

func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
// fetch the asset for the album
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

var picturePlaceHolder: PHObjectPlaceholder? = nil

if let _: AnyObject = collection.firstObject {
return collection.firstObject
}
return nil
}

func save(image: UIImage) {
if assetCollection == nil {
return
}

PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)

}, completionHandler: nil)
}

Answer Source

I posted a similar question: Swift 3 or 4 Saving to custom album creates duplicate images

But I got nothing but crickets as well. Luckily, I think I found the answer. I'll answer my own question as well.

The code you have (which was the same code I had) is to CREATE A NEW ASSET. It is useful only for the saving the image to your custom album after the user has taken a picture with the camera. It is for brand new assets.

However, for existing assets, you do not want to create a new asset. Instead, you want to add the existing asset to the custom album. To do this, you need a different method. Here is the code I created and it seems to be working. Keep in mind that you will have to get the asset ID FIRST, so that you can send it to your method and access the existing asset.

So, in your imagePickerController, you have to determine whether the user chose an existing image or whether the method is being called from a new camera action.

let pickerSource = picker.sourceType;
switch(pickerSource){
 case .savedPhotosAlbum, .photoLibrary:
  if(let url = info[UIIMagePickerControllerReferenceURL] as? NSURL{
   let refURLString = refURL?.absoluteString;
   /* value for refURLString looks something like assets-library://asset/asset.JPG?id=82A6E75C-EA55-4C3A-A988-4BF8C7F3F8F5&ext=JPG */
   let refID = {function here to extract the id query param from the url string}
   /*above gets you the asset ID, you can get the asset directly, but it is only 
     available in ios 11+.
   */
   MYPHOTOHELPERCLASS.transferImage(toAlbum: "myalbumname", withID: refID!, ...)

 }
 break;
 case .camera:
 ...
 break;
}

Now, in your photohelper class (or in any function anywhere, whatever), to EDIT the asset instead of create a new one, this is what I have. I am assuming the changeRequest variable can be ommitted. I was just playing around until I got this right. Going through the completely ridiculous apple docs I was able to at least notice that there were other methods to play with. I found that the NSFastEnumeration parameter can be an NSArray of PHAssets, and not just placeholder PHObjectPlaceholder objects.

public static func transferImage(toAlbum albumName:String, withID imageID:String, onSuccess success:@escaping(String)->Void, onFailure failure:@escaping(Error?)->Void){

  guard let album = self.getAlbum(withName: albumName) else{
    ... failure here, albumNotFoundError
    return;
  }

  if(self.hasImageInAlbum(withIdentifier: imageID, fromAlbum: albunName)){
    ... failure here, image already exists in the album, do not make another
    return;
  }

  let theAsset = self.getExistingAsset(withLocalIdentifier: imageID);
  if(theAsset == nil){
    ... failure, no asset for asset id
    return;
  }

    PHPhotoLibrary.shared().performChanges({
      let albumChangeRequest = PHAssetCollectionChangeRequest(for: album);
      let changeRequest = PHAssetChangeRequest.init(for: theAsset!);
      let enumeration:NSArray = [theAsset!];
      let cnt = album.estimatedAssetCount;
      if(cnt == 0){
          albumChangeRequest?.addAssets(enumeration);
      }else{
          albumChangeRequest?.inserAssets(enumeration, at: [0]);
      }
    }){didSucceed, error) in
       OperationQueue.main.addOperation({
         didSucceed ? success(imageID) : failure(error);
       })
    }

}

So, it is pretty much the same, except instead of creating an Asset Creation Request and generating a placeholder for the created asset, you instead just use the existing asset ID to fetch an existing asset and add the existing asset to the addasset/insertasset NSArray parameter instead of a newly created asset placeholder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download