OS X image editing action extension has no affect in Mail.app

I'm working on action extension for OS X that edits single image.


Everything works well in TextEdit.app. But image changes has no affect in Mail.app. I'm using this code to return edited image back to host app:

- (IBAction)done:(id)sender
{
    NSExtensionItem *outputItem = [[NSExtensionItem alloc] init];
    outputItem.attachments = @[self.imageView.image];
    NSArray *outputItems = @[outputItem];
    [self.extensionContext completeRequestReturningItems:outputItems completionHandler:nil];
}


In extension and in Xcode I can see that image is changing. But after [self.extensionContext completeRequestReturningItems:outputItems completionHandler:nil] nothing happens in Mail.app.


Here's project:

https://github.com/derpoliuk/TestExtensionApp/


I created bug report:

rdar://21358060


I saw that other apps are editing photo in Mail.app (for example Pixelmator - http://www.idownloadblog.com/2015/04/21/pixelmator-repair-tool-extension-mac/). So there's should be something that I'm doing wrong.


Any help would be really appreciated.


Stanislav

Answered by derpoliuk in 12645022

To make it work one should pass back NSData in NSItemProvider with kUTTypePNG identifier:


NSImage *image = self.imageView.image;
NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = @{NSImageCompressionFactor: @1.0};
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithItem:imageData typeIdentifier:(NSString *)kUTTypePNG];
NSExtensionItem *outputItem = [[NSExtensionItem alloc] init];
outputItem.attachments = @[itemProvider];
[self.extensionContext completeRequestReturningItems:@[outputItem] completionHandler:nil];


Stanislav

Haven't done an action extension in a bit. See this old thread: https://devforums.apple.com/message/1105124#1105124


There's a post in there from someone who says images will work in Mail if you pass back NSData for the image instead of an NSImage object. The poster claims though, that passing back NSData in TextEdit will crash the app. No idea if they made any changes since or 'patched it'.


Maybe Pixelmater passes back NSData.... you can try that and see if it works in TextEdit too (if indeed it does work..again I didn't try it myself).

Thank you for link. Forgot about "when" search criteria.


Indeed NSData works for Mail, but it crashes TextEdit.


Stanislav

Accepted Answer

To make it work one should pass back NSData in NSItemProvider with kUTTypePNG identifier:


NSImage *image = self.imageView.image;
NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = @{NSImageCompressionFactor: @1.0};
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithItem:imageData typeIdentifier:(NSString *)kUTTypePNG];
NSExtensionItem *outputItem = [[NSExtensionItem alloc] init];
outputItem.attachments = @[itemProvider];
[self.extensionContext completeRequestReturningItems:@[outputItem] completionHandler:nil];


Stanislav

OS X image editing action extension has no affect in Mail.app
 
 
Q