【发布时间】:2016-12-27 07:23:34
【问题描述】:
我正在制作照片扩展,但是当我尝试保存更改时(点击完成按钮)。
警报消息显示“无法保存更改”-“发生错误” 在保存的同时。请稍后再试。'
这是我的代码 finishContentEditingWithCompletionHandler:completionHandler
- (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(QOS_CLASS_DEFAULT, 0), ^{
// Create editing output from the editing input.
PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];
NSURL *originalImageURL = self.input.fullSizeImageURL;
//Apply filter
CIFilter *appliedFilter = [CIFilter filterWithLUT:self.presetFilterNameArray[self.currentAppliedFilterIndex] originalLUT:@"LUT-ORG-512" dimension:64 alpha:self.filterAlphaSlider.value CIContext:self.ciContext];
UIImage *filteredOriginalImage = [self filterApply:[UIImage imageWithContentsOfFile:originalImageURL.path] filter:appliedFilter];
//Apply orientation
filteredOriginalImage = [UIImage imageWithCGImage:filteredOriginalImage.CGImage scale:filteredOriginalImage.scale orientation:self.input.fullSizeImageOrientation];
// Provide new adjustments and render output to given location.
NSData *archiver = [NSKeyedArchiver archivedDataWithRootObject:self.presetFilterNameArray[self.currentAppliedFilterIndex]];
output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.fantagram.BetterAndBetter.TKPhotoExtension" formatVersion:@"1.0" data:archiver];
NSData *renderedJPEGData = UIImageJPEGRepresentation(filteredOriginalImage, 1.0f);
if([renderedJPEGData writeToURL:output.renderedContentURL atomically:YES]){
NSLog(@"success to write");
}
else{
NSLog(@"fail to write");
}
NSLog(@"%@", output);
// Call completion handler to commit edit to Photos.
completionHandler(output);
// Clean up temporary files, etc.
});
}
上一个回答这个问题,有人说渲染到NSData时,图片的大小需要和originalImage不同。所以我尝试了这个,但没有奏效。
还有一件事。
有没有办法从照片扩展打开主机应用程序?
self.extensionContext openURL: 不起作用(据我所知,它只起作用 在今日扩展中。)
UIResponder *responder = self;
while(responder){
if([responder respondsToSelector:@selector(openURL:)]){
dispatch_async(dispatch_get_main_queue(), ^{
[responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:(MY_URL_Schemes)]];
});
}
responder = [responder nextResponder];
}
上面的代码也不起作用。当我使用它时它工作了share extension。
谢谢。
【问题讨论】:
-
关于您的第二个问题:它在文档中,但是可以从扩展中使用的 iOS SDK 的 API 是有限的。特别是,整个
UIApplication业务超出了界限(这是有道理的:您的扩展只是一个共享框架,被另一个应用程序使用,它拥有sharedInstance的UIApplication)。因此,我认为您不能从扩展程序中启动容器应用程序。 -
我根据Apple的示例代码创建了一个最小的应用程序,只是它尝试将原始图像保存不变;
finishContentEditing:的其余部分与 Apple 的代码相同。我已经记录了编辑会话开始时传递的PHContentEditingInput数据的所有属性,我很快注意到当fullSizeImageOrientation的值为6(又名CGImagePropertyOrientation.right)时,保存总是失败,这在某种程度上适用于所有照片我接受纵向。一旦我找到解决方法或解决方案,我会发布一个。回答。
标签: ios objective-c ios-extensions