2

OpalImagePickerController用来从“所有照片”/“相机胶卷”中挑选图像/视频,[PHAsset]这很好,但我想将它们添加到照片应用程序中的特定相册中,为此我使用以下代码:

@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")
    })
}

这是我正在使用的辅助方法:

//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)
    }
}

我的问题是它在“所有照片”/“相机胶卷”中重复图像/视频。

我还发现了一些关于它的帖子,例如:

将 PHAsset 从一张专辑移动/复制到另一张专辑

但这对我没有帮助。

4

1 回答 1

2

您在此处请求原始文件 -

// 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

然后在这里创建一个新的/重复的资产 -

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

我们对创建新专辑不感兴趣PHAsset——我们将现有专辑链接PHAsset到专辑。你需要做的很简单 -

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))")
})
于 2021-06-29T11:41:21.157 回答