首次运行时将照片保存在自定义相册中失败
Posted
技术标签:
【中文标题】首次运行时将照片保存在自定义相册中失败【英文标题】:Saving a photo in a custom album fails when run for the first time 【发布时间】:2017-02-18 07:12:33 【问题描述】:您好,我正在创建一个相册应用程序作为一个学习项目,我在使用 phphotoLibrary.shared().performChangesAndWait
时遇到了一些问题;主要问题是当方法 saveImage 具有PHPhotoLibrary.shared().performChangesAndWait
代码时;该方法在PHPhotoLibrary.shared().performChangesAndWait
完成保存图像之前退出。即使我使用 performChangesAndWait
方法,似乎代码也会异步执行,根据文档说它应该等到它完成。
这是我的类中 saveImage 方法的代码:
func saveImage(_ image: UIImage) ->Bool
print("Executing saveImage")
var result:Bool = false
if assetCollection == nil
print("the album is nil")
return result
print("saveImage: we will start saving the image")
//TODO: the return false is happening before the photo is save.
do
try PHPhotoLibrary.shared().performChangesAndWait(
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
print("saveImage: image was save without issues")
result = true
print("saveImage: result value after saving the image: \(result)")
)
catch let error
result = false
print("saveImage: there was a problem: \(error.localizedDescription)")
print("saveImage: result value before exiting the method: \(result)")
return result
基本上再次在应用程序第一次运行时返回结果在保存图像块完成之前执行。
有什么方法可以强制 saveImage 方法实际等到 performChangesAndWait 方法实际完成?
提前感谢您的帮助。
【问题讨论】:
【参考方案1】:performChangesAndWait
是一个异步回调方法。在 performChangesAndWait
内部对 result
所做的更改不会反映在 return 上,因为这两个位于不同的线程中。
等待saveImage
方法完成的一种方法是实现回调。比如:
func saveImage(_ image: UIImage, callback: @escaping(_ result: Bool)->Void)
print("Executing saveImage")
if assetCollection == nil
print("the album is nil")
callback(false)
print("saveImage: we will start saving the image")
//TODO: the return false is happening before the photo is save.
do
try PHPhotoLibrary.shared().performChangesAndWait(
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
print("saveImage: image was save without issues")
callback(true)
)
catch let error
print("saveImage: there was a problem: \(error.localizedDescription)")
callback(false)
让我们知道你得到了什么。
【讨论】:
我了解此解决方案在您的“结果”布尔值中为您提供了良好的结果反馈,但这如何阻止第一个 saveImage 失败? @drewster,问题不在于保存图像功能,而是刷新问题。【参考方案2】:这是解决方案(Swift 5):
@objc private func handleSavePhoto()
guard let previewImage = self.photoImageView.image else return
PHPhotoLibrary.requestAuthorization (status) in
if status == .authorized
do
try PHPhotoLibrary.shared().performChangesAndWait
PHAssetChangeRequest.creationRequestForAsset(from: previewImage)
catch let error
print("failed to save photo in library: ", error)
else
print("Something went wrong with permission...")
【讨论】:
以上是关于首次运行时将照片保存在自定义相册中失败的主要内容,如果未能解决你的问题,请参考以下文章