ios - 无法使用 PHPhotoLibrary 将 UIImage 保存到相机胶卷

标签 ios swift uiimage phphotolibrary

我正在尝试将 UIImage 保存到相机胶卷。 Apple 弃用了 UIImageWriteToSavedPhotosAlbum,因此我避免使用它(ALAssets 也是如此),它会强制使用 PhotoLibrary

这是我正在使用的代码:

定义:

var rollCollection : PHAssetCollection!;

初始化:

let result = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil);
rollCollection = result.firstObject as? PHAssetCollection;

图片保存代码:

    if (rollCollection != nil){
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(
                self.originalImg!);

            let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.rollCollection!);
            let assetPlaceHolder = assetRequest.placeholderForCreatedAsset;
            albumChangeRequest!.addAssets([assetPlaceHolder!])

            }, completionHandler: { success, error in
                dispatch_async(dispatch_get_main_queue(), {
                    print ("\(error!)");
                    if (!success){
                        self.presentViewController(self.alertCantSave!, animated: false, completion: nil);
                    }
                    else {
                        self.presentViewController(self.alertSaved!, animated: false, completion: nil);
                    }
                });

        })
    }
    else {
        dispatch_async(dispatch_get_main_queue(), {
        self.presentViewController(self.alertCantSave!, animated: false, completion: nil);
        });
    }

每次我尝试保存图像时,我都会收到以下错误:

Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

有什么想法吗? 我找不到任何解释,到处寻找,我发现了一个类似于我的代码片段(包括 Apple 的文档:https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/)

谢谢。

最佳答案

我在寻找完全相同的东西几个小时后才发现这个问题。

经过一番尝试,我得出了一个对我有用的答案,所以我在这里分享它,以防其他人在未来寻找类似的东西。

// Make sure the image is not nil
guard let image = image else {
  // No image here...
  return
}

// Perform changes to the library 
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
  // You only need to create the request, there is no need to define the placeholder 
  // nor to add the asset because swift does not like it when you try to modify the camera roll.
  PHAssetChangeRequest.creationRequestForAssetFromImage(image)

}, completionHandler: { success, error in
  // Same as what you have...

})

如果您想了解如何将图像保存到自定义相册以及从自定义相册中查询图像的示例,请查看此答案。 Save images with PHImageManager

关于ios - 无法使用 PHPhotoLibrary 将 UIImage 保存到相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36511534/

相关文章:

ios - 使用 Swift 以编程方式加载图像

ios - 运行 Ionic 构建时收到 NSUnknownKeyException 错误

ios - 如何将我现有的项目更改为仅适用于 iPhone?

iphone - 使用 AUGraphs 进行麦克风输入

ios - 是什么原因导致这种情况发生? NSUserDefaults 无法在 watchkit 项目中的 IOS 和 Watch 之间共享数据

ios - 如何计算特定 iPhone 上的像素到点的转换?

objective-c - 删除 NSString 中的特殊字符

swift - 'Set' <NSObject> 没有名为 'anyObject' 的成员

ios - alamofire 没有准备正确的 url

ios - CIMedianFilter 是如何工作的? (算法)