我正在将应用程序移植到 Catalyst。该应用程序有一个功能,可以在 Catalyst 环境中将屏幕截图保存到命名相册,在 iOS 环境中保存到相机胶卷。该代码运行良好,并产生一个没有错误的“成功”代码。如果我查看收藏的估计资产数量,每次尝试后它都会增加。但是,在“照片”应用中查看时,相册中没有内容。我的截图在哪里? 以下是相关代码:
func insertImage(image : UIImage, intoAssetCollection collection : PHAssetCollection) {
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.creationRequestForAsset(from: image)
let request = PHAssetCollectionChangeRequest(for: collection)
if request != nil && creationRequest.placeholderForCreatedAsset != nil {
request!.addAssets([creationRequest.placeholderForCreatedAsset!] as NSFastEnumeration)
}
},
completionHandler: { (success, error) in
if error != nil {
print("Error: " + error!.localizedDescription)
DispatchQueue.main.async {
let ac = UIAlertController(title: "Save error", message: error!.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Done", style: .default))
self.present(ac, animated: true)
}
} else {
DispatchQueue.main.async {
let ac = UIAlertController(title: "Save success", message: "Image saved", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Done", style: .default))
self.present(ac, animated: true)
}
}
}
)
专辑收藏来自:
func fetchAssetCollectionWithAlbumName(albumName : String) -> PHAssetCollection? {
let fetchOption = PHFetchOptions()
fetchOption.predicate = NSPredicate(format: "title == '" + albumName + "'")
let fetchResult = PHAssetCollection.fetchAssetCollections(
with: PHAssetCollectionType.album,
subtype: PHAssetCollectionSubtype.albumRegular,
options: fetchOption)
let collection = fetchResult.firstObject
return collection
}