如何区分为 `UIDocumentInteractionController` "Open in" 菜单选择的操作

How to differentiate actions selected for `UIDocumentInteractionController` "Open in" menu

我正在尝试使用 UIDocumentInteractionController 轻松共享我的应用程序中存在的图像。使用我正在使用的以下代码,一切看起来都很好。 (图片可以转发到whatsapp、viber、instagram。图片可以保存到相机胶卷等)

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

那么您认为有没有办法为不同的菜单项操作提供不同的图像?还是必须先出示actioncontroller然后说"open in instagram"、"open in rest"?如果 whatsapp、twitter、facebook 等其他应用程序具有独有的 "open in" UTI,我就会那样做。

顺便问一下,您知道为什么 facebook 没有显示在列表中吗? (我查看了 edit/manage 列表。它不在那里)

    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];

您需要实现委托的方法。

- (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];

   }
}