如何将 PHAsset 的引用从 "All Photos"/"Camera Roll" 移动到特定的 PHAssetCollection?
How to move reference of PHAsset from "All Photos"/"Camera Roll" to Specific PHAssetCollection?
我正在使用 OpalImagePickerController
从“所有照片”/“相机胶卷”中选择 images/videos,它给了我 [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)
}
}
我的问题是它在“所有照片”/“相机胶卷”中重复 images/videos。
我还发现了一些 post 类似的东西:
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
然后在此处创建 new/duplicate 资产 -
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))")
})
我正在使用 OpalImagePickerController
从“所有照片”/“相机胶卷”中选择 images/videos,它给了我 [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)
}
}
我的问题是它在“所有照片”/“相机胶卷”中重复 images/videos。
我还发现了一些 post 类似的东西:
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
然后在此处创建 new/duplicate 资产 -
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))")
})