【问题标题】:iOS photo extension finishContentEditingWithCompletionHandler: Unable to Save ChangesiOS照片扩展finishContentEditingWithCompletionHandler:无法保存更改
【发布时间】:2016-01-29 10:18:56
【问题描述】:

我的照片扩展应用可以访问相机和照片。 一切正常,但按完成时,无法保存图像。

标准完成处理程序代码:

- (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler {
    // Update UI to reflect that editing has finished and output is being rendered.

    // Render and provide output on a background queue.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];

        NSError* error = nil;

        NSData *renderedJPEGData = UIImageJPEGRepresentation(filtered_ui_image, 1.0);
        assert(renderedJPEGData != nil);
        //BOOL written_well = [renderedJPEGData writeToURL:output.renderedContentURL atomically:YES];

        BOOL written_well = [renderedJPEGData writeToURL:output.renderedContentURL options:NSDataWritingAtomic error:&error];
        assert(written_well);



        // Call completion handler to commit edit to Photos.
        completionHandler(output);
    });
}

renderedJPEGData 不是nil
errornil,因此函数[NSData writeToURL] 成功,
written_wellYES

在逐行调试时,块完成后,会出现警告:

output.renderedContentURL/private/var/mobile/Containers/Data/PluginKitPlugin/509C1A04-D414-4DB7-B1E6-83C47FC88BC9/tmp/blah_blah_name.JPG

那么,我有权限,调试显示没有错误,我可以尝试检测问题的原因是什么?

【问题讨论】:

    标签: ios file-io photo-gallery


    【解决方案1】:

    从 iOS 10 开始,调整数据必须至少有一个字节。这是与 iOS 9 相比的重大变化,其中调整数据可以为零。我已经在 iOS 9 和 iOS 10 上对此进行了测试以确认。

    其他文档:https://developer.apple.com/reference/photos/phcontenteditingoutput/1518684-adjustmentdata

    PHContentEditingOutput* output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];
    NSMutableData* adjustmentData = [NSMutableData data];
    uint8_t byte = 1;
    [adjustmentData appendBytes:&byte length:1];
    output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.yourcompany.yourapp" formatVersion:@"1.0f" data:adjustmentData];
    

    【讨论】:

    • 这对我不起作用。我只收到一些图像的错误,每次都是相同的图像。其他图像保存没有问题。无法指出保存的图像和不保存的图像之间有什么区别...
    【解决方案2】:

    尽管标题暗示 adjustData 可以为 nil,但文档指出:

    如果您将新的资产内容写入到 renderContentURL 属性指定的 URL,您还必须提供一个新的、不同的 PHAdjustmentData 对象来描述您的编辑。传递预先存在的调整数据对象(描述早期编辑)会导致未定义的行为。

    所以在调用完成处理程序之前做这样的事情:

    output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.whatever.app" formatVersion:@"1.0" data:[NSData data]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多