我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)
}
}
我的问题是它在“所有照片”/“相机胶卷”中重复图像/视频。
我还发现了一些关于它的帖子,例如:
但这对我没有帮助。