代码之家  ›  专栏  ›  技术社区  ›  olha

iOS照片扩展finishContentEditingWithCompletionHandler:无法保存更改

  •  8
  • olha  · 技术社区  · 9 年前

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

    标准完成处理程序代码:

    - (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 ,
    error ,因此功能 [NSData writeToURL] 成功,
    written_well YES ,

    逐行调试时,在块完成后,会出现一条警告: enter image description here

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

    所以,我有权限,调试没有显示错误,我可以尝试检测问题的原因吗?

    2 回复  |  直到 9 年前
        1
  •  7
  •   jjxtra    8 年前

    从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
  •  1
  •   chockenberry    9 年前

    尽管标题暗示adjustmentData可以为零,但文档说明:

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

    因此,在调用完成处理程序之前,请执行以下操作:

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