将图像保存到照片库时出错 iOS/Mac Catalyst 应用程序 Swift

Error Saving Image to Photo Library iOS/Mac Catalyst App Swift

我构建了一个 iOS 应用程序,可以将图像从 UIImageView 保存到用户的照片库中。 这适用于 iOS,但是当我尝试在 iOS 13/macOS Catalina 中使用 Apple 的 Mac Catalyst 产品向此应用程序添加对 Mac 的支持时,图像不会保存我看到了一条错误消息。

我用来保存照片的代码复制如下:

@objc func export(sender:AnyObject) {
    UIImageWriteToSavedPhotosAlbum(self.previewImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        print("Error saving: \(error)")
        let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    } else {
        let ac = UIAlertController(title: "Saved", message: "Your image has been saved to your photos.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    }
}

警报视图中的错误消息是 "Unknown Error",但这是 Xcode 控制台中打印的错误消息:

[GatekeeperXPC] XPC connection error to assetsd getSystemLibraryURLReadOnlyServiceWithReply: : Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service on pid 0 named com.apple.photos.service was invalidated." UserInfo={NSDebugDescription=The connection to service on pid 0 named com.apple.photos.service was invalidated.}

Error saving: Error Domain=ALAssetsLibraryErrorDomain Code=-1 "Unknown error" UserInfo={NSLocalizedDescription=Unknown error, NSUnderlyingError=0x600000cfe4f0 {Error Domain=ALAssetsLibraryErrorDomain Code=-1 "Unknown error" UserInfo={NSLocalizedDescription=Unknown error, NSUnderlyingError=0x600000d01680 {Error Domain=ALAssetsLibraryErrorDomain Code=-1 "Unknown error" UserInfo={NSLocalizedDescription=Unknown error, NSUnderlyingError=0x600000d03930 {Error Domain=com.apple.photos.error Code=41002 "Unable to obtain photolibraryd XPC proxy for getResourceWriteOnlyServiceWithReply:. photolibraryd could have crashed" UserInfo=0x6000017be600 (not displayed)}}}}}}

你知道为什么会出现这个错误吗?如果有什么我可以做的来获得更多关于如何修复这个错误的信息或者我可以尝试的其他任何事情?

谢谢。

我能够通过此 Mac Catalyst 应用程序使用 Swift;

中的以下代码将图像保存到 MacOS
func insertImageMac(image : UIImage, albumName : String) {
    let collection = fetchAssetCollectionWithAlbumName(albumName: albumName)
    if collection == nil {
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
            }, completionHandler: {(success, error) in
                if error != nil {
                    print("Error: " + error!.localizedDescription)
                }

                if success {
                    let newCollection = self.fetchAssetCollectionWithAlbumName(albumName: albumName)
                    self.insertImage(image: image, intoAssetCollection: newCollection!)
                }
            }
        )
    } else {
        self.insertImage(image: image, intoAssetCollection: collection!)
    }
}

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 as? PHAssetCollection

    return collection
}

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)
                let ac = UIAlertController(title: "Save error", message: error!.localizedDescription, preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Done", style: .default))
                self.present(ac, animated: true)
            } else {
                let ac = UIAlertController(title: "Save success", message: "Image saved", preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Done", style: .default))
                self.present(ac, animated: true)
            }
        }
    )
}

...并在我的按钮函数中使用图像调用它;

insertImageMac(image: myImage, albumName: "AlbumName")