Reputation: 3786
When I try to create a share extension (or action extension) to write email containing shared images, why do the larger images not appear, only small ones?
Upvotes: 2
Views: 4068
Reputation: 3786
Because there may be a delay before image data is provided to your extension, you must wait for it to arrive before presenting an email compose window including them. (Otherwise they do not appear)
I keep a counter in bitsToLoad, that I increment for each piece of data I request, and decrement for each piece I receive
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
bitsToLoad ++;
__weak MFMailComposeViewController *pick = picker;
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
if (image) {
[pick addAttachmentData:UIImageJPEGRepresentation(image,0.8) mimeType:@"image/jpeg" fileName:@"image.jpg"];
}
[self dataFetched];
}];
And check that they have all arrived, before I present the Email form.
-(void) dataFetched {
bitsToLoad--;
if (bitsToLoad==0) {
[self presentViewController:picker animated:YES completion:^(){ }];
}
}
But this isn't quite enough.
To make sure the email interface does not appear until all attachments have been considered, AND that it will be presented at the end even if there were no images, I set bitsToLoad to 1 at the beginning of the scanning routine...
NSInteger bitsToLoad=1
and decrement and check it at the end.
[self dataFetched];
Now the view controller will only be presented after all attachments have been checked, and all data has been fetched.
Note that you may find that small images (such as screen grabs) appear even without this sort of check, as they are passed to the extension quickly enough, but it would definitely break without this for larger images, such as typical iPhone photos.
Upvotes: 1