3

我试图使用UIDocumentInteractionController实现在我的应用程序中存在的图像轻松共享。用我使用的下面的代码,一切看起来都很好。 (图像可以被转发到WHATSAPP,Viber的,Instagram的。图像可以被保存到相机胶卷等)如何区分为“UIDocumentInteractionController”选择的操作“Open in”菜单

open in and options sheet

然而,我需要一些在菜单中的操作之间进行区分。这是因为,例如Instagram只接受方形图像,并且在将图像传送给instagram之前需要进行预处理。 (Instagram的工程师应该从裁剪屏幕而不是过滤器开始应用!@#!$)否则instagram auto会自动裁剪图像的底部或右侧部分。

那么你认为有一种方法可以为不同的菜单项操作提供不同的图像吗?或者我必须首先提出一个行动控制器,并说“在instagram中打开”,“在休息时打开”?如果像whatsapp,twitter,facebook等其他应用程序拥有独家“开放式”UTI,我会走这条路。

顺便说一下,你有什么想法为什么Facebook不在列表中的showns? (我看着编辑/管理列表,它不在那里)

NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    //NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.png"]; 
    NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.ig"]; 
    //NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.igo"]; // *.igo is exclusive to instagram 
    NSData *imageData = UIImagePNGRepresentation(capsObject.capImage); 
    [imageData writeToFile:saveImagePath atomically:YES]; 
    NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath]; 

    self.docController=[[UIDocumentInteractionController alloc]init]; 
    self.docController.delegate = self; 
    //self.docController.UTI = @"public.image"; 
    self.docController.UTI = @"com.instagram.photo"; 
    //self.docController.UTI = @"com.instagram.exclusivegram"; 
    [self.docController setURL:imageURL]; 
    //[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
    [self.docController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES]; 

回答

0

您需要实现委托的方法。

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application{ 
    NSLog(@"Will beginsending to application"); 
// Detect if the application is instagram 

    if(![application isEqualToString:@"com.instagram.photo"]){ 
    //Make the custom implementation of your image. 
    UIImage  * iconImage = [self prepareImage]; 
    //save it to documents path 
    NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Image.ig"]; 
    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES]; 
    // change the URL of your file 
    controller.URL=[NSURL fileURLWithPath:savePath]; 

    } 
} 
+0

该功能仅在点击“在应用程序中打开”时才会调用。你有共享和动作扩展的解决方案吗? – jtmayer 2016-06-22 12:45:34