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