Search code examples
iosswiftphassetphassetcollectionphassetslibrary

How to move reference of PHAsset from "All Photos"/"Camera Roll" to Specific PHAssetCollection?


I am using OpalImagePickerController to pick images/videos from "All Photos"/"Camera Roll" and it's giving me [PHAsset] which is fine but I wanted to add them to Specific album in Photos app and I am using below code for that:

@objc func btnAddTapped() {
    
    guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else {
        //Show error to user?
        return
    }

    //Example Instantiating OpalImagePickerController with Closures
    let imagePicker = OpalImagePickerController()

    //Present Image Picker
    presentOpalImagePickerController(imagePicker, animated: true, select: { (selectedAssets) in
        
        imagePicker.dismiss(animated: true, completion: nil)
        
        let sdLoader = SDLoader()
        sdLoader.startAnimating(atView: self.view)
        
        let options = PHImageRequestOptions()
        options.deliveryMode = .highQualityFormat
        options.isNetworkAccessAllowed = true
        
        let videoOptions = PHVideoRequestOptions()
        videoOptions.deliveryMode = .highQualityFormat
        videoOptions.isNetworkAccessAllowed = true
        
        let myGroup = DispatchGroup()
        
        for item in selectedAssets {
            
            myGroup.enter()
            
            let asset = item
            if asset.mediaType == PHAssetMediaType.video {
                //Fetch URL if its a video
                PHCachingImageManager().requestAVAsset(forVideo: asset, options: videoOptions) { (playerItem, audioMix, args) in
                    
                    if let videoAVAsset = playerItem as? AVURLAsset {
                        let url = videoAVAsset.url
                        PhotoManager.instance.storeVideoToSpecificAlbum(videoURL: url, to: self.asset, completion: {_ in
                            print("video stored")
                            myGroup.leave()
                        })
                    }
                }
                
            } else {
                //Image
                PHImageManager.default().requestImageDataAndOrientation(for: asset, options: options, resultHandler: {(data, string, orientation, any) in
                    if let data = data {
                        DispatchQueue.main.async {
                            if let img = UIImage(data: data) {
                                PhotoManager.instance.storeImageToSpecificAlbum(image: img, to: self.asset, completion: {_ in
                                    print("Image stored")
                                    myGroup.leave()
                                })
                            }
                        }
                    }
                })
            }
        }
        
        myGroup.notify(queue: .main) {
            sdLoader.stopAnimation()
            self.fetchImagesFromAlbum()
        }
        
    }, cancel: {
        print("cancel tapped")
    })
}

And here is the helper methods I am using:

//Store Video
func storeVideoToSpecificAlbum(videoURL: URL, to album: PHAssetCollection, completion: @escaping (PHAssetCollection?) -> Void) {
    
    PHPhotoLibrary.shared().performChanges({
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL)
        let placeholder = assetRequest?.placeholderForCreatedAsset
        guard let _placeholder = placeholder else { completion(nil); return }
        
        let albumChangeRequest = PHAssetCollectionChangeRequest(for: album)
        albumChangeRequest?.addAssets([_placeholder] as NSFastEnumeration)
    }) { success, error in
        completion(album)
    }
}

//Store Image
func storeImageToSpecificAlbum(image: UIImage, to album: PHAssetCollection, completion: @escaping (PHAssetCollection?) -> Void) {
    
    PHPhotoLibrary.shared().performChanges({
        let assetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        let placeholder = assetRequest.placeholderForCreatedAsset
        guard let _placeholder = placeholder else { completion(nil); return }
        
        let albumChangeRequest = PHAssetCollectionChangeRequest(for: album)
        albumChangeRequest?.addAssets([_placeholder] as NSFastEnumeration)
    }) { success, error in
        completion(nil)
    }
}

My problem is it duplicate images/videos in "All Photos"/"Camera Roll".

I also found some post about it like:

Move/copy PHAsset from one album to another

But it doesn't helped me.


Solution

  • You are requesting the original files here -

    // Video
    PHCachingImageManager().requestAVAsset(forVideo: asset, options: videoOptions) { (playerItem, audioMix, args) in
    
    // Image
    PHImageManager.default().requestImageDataAndOrientation(for: asset, options: options, resultHandler: { (data, string, orientation, any) in
    

    and then creating a new/duplicate asset here -

    let assetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
    let placeholder = assetRequest.placeholderForCreatedAsset
    

    We are not interested in creating a new PHAsset - we are linking an existing PHAsset to an album. What you need to do is as simple as -

    PHPhotoLibrary.shared().performChanges({
        let albumUpdateRequest = PHAssetCollectionChangeRequest(for: album) // album you plan to update
        albumUpdateRequest?.addAssets(NSArray(array: [asset])) // asset you plan to add
    }, completionHandler: { (completed, error) in
        print("completed: \(completed), error: \(String(describing: error))")
    })