iOS Error Domain=PHPhotosErrorDomain Code=3300

AVCapturePhoto的数据保存到 PHPhotoLibrary的时候报错Error Domain=PHPhotosErrorDomain Code=3300

解决代码(也可以使用addResourceWithType:data:options:来添加数据到request,JEPG的实测可以,raw的不确定):

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];

// self.rawFileURL is your custom URL, you'd better to use "[yourFileDirectoryAndFileName URLByAppendingPathExtension:@"DNG"];" to express format
[request addResourceWithType:PHAssetResourceTypeAlternatePhoto fileURL:self.rawFileURL options:options];

// self.compressedFileURL is your custom URL, you can choose some compress format to save. It's optional, but if you invoke "AVCapturePhotoSettings photoSettingsWithRawPixelFormatType:processedFormat:",you must pass parameter "processedFormat" but not need to invoke addResourceWithType:fileURL:options: as below:
if (self.compressedFileURL) {
    [request addResourceWithType:PHAssetResourceTypePhoto fileURL:self.compressedFileURL options:nil];
}

 运行截图:

问题原因:保存RAW到resource的时候用了PHAssetResourceTypePhoto,但是PHAssetResourceType的文档显示raw用PHAssetResourceTypeAlternatePhoto(下图最后一行).这和苹果的官方示例是不一样的,注意注意!

如果你只想保存RAW文件,你也需要调用"AVCapturePhotoSettings photoSettingsWithRawPixelFormatType:processedFormat:",注意你必须传递参数给processedFormat,但接下来不需要调用addResourceWithType:fileURL:options:添加有损格式(例如JPEG)到照片库PHPhotoLibrary。

还有PHAssetCreationRequest和PHAssetResourceCreationOptions对象必须只新建一次,如果多次alloc和init这两个对象将导致问题。(好像是code=3301)

你可能感兴趣的:(ios)