ios - 如何将 PHAsset 的引用从 "All Photos"/"Camera Roll"移动到特定的 PHAssetCollection?

标签 ios swift phasset phassetcollection phassetslibrary

我正在使用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)
    }
}

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

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

Move/copy PHAsset from one album to another

但这对我没有帮助。

最佳答案

您在此处索取原始文件 -

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

关于ios - 如何将 PHAsset 的引用从 "All Photos"/"Camera Roll"移动到特定的 PHAssetCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68177312/

相关文章:

ios - 处理可观察序列 RxSwift 上的循环样式事件

ios - 使用 PHAsset 获取用户照片库时如何分页

ios - 在 iOS 9 RequestImageForAsset 返回小图像检索 1 :1 photos with filters

ios - UIPageViewController最后一页跳动效果?

ios - 设置 View Controller 实例的 UIImageView

ios - Facebook Audience Network 在 XCode 7 上有太多警告

ios - 在所有节点 SCNLight 上转换阴影

iOS 应用无法运行 : unable to dequeue a cell with identifier Cell

ios - 让多个 UIButton 共享相同外观的最佳方法

ios - 使用 PHFetchResult 的 `enumerateObjectsUsingBlock` 时出现编译器错误