2014-10-16 74 views
0

所以我有我的應用程序創建一個文件,在這種情況下,它實際上是一個動畫.gif。這個動畫的.gif保存在設備上,並且已準備就緒 - 我現在要做的就是在用戶單擊「共享」按鈕時共享此文件。如何與iOS共享文件?

簡而言之,我該如何做到這一點?

有沒有辦法告訴iOS打開共享窗口,我分享圖像?現在我找不到這樣做的方法,因此我正在創建一個新的UIImage,並在設備上爲此動畫設置了一個.gif路徑,但這最終會將我的動畫文件轉換爲靜態圖像。

再次 - 我已經創建並保存在用戶的設備上的動畫.gif文件

任何幫助表示讚賞。

編輯:爲了澄清,我習慣了Android,我只是想調用一個共享文件的意圖,然後彈出一個Gmail,Facebook,Twitter等等的列表......我基本上想要相當於如果可能的話。

+0

'UIDocumentInteractionController'。 – rmaddy 2014-10-16 22:25:23

+0

分享什麼?一個文件需要一個應用程序才能運行。命名一個。 – 2014-10-17 04:14:22

+1

請參閱我習慣了Android,我只是想調用一個共享文件的意圖,然後彈出一個Gmail,Facebook,Twitter等等的列表......如果可能的話,我基本上都希望獲得相同的結果。 – FTLRalph 2014-10-17 11:38:35

回答

1

您可能想看看UIActivityViewController文檔。這將打開一張表格,其中包含可在設備上共享的所有可用方式。在這裏看到https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivityViewController_Class/index.html#//apple_ref/doc/uid/TP40011976-CH1-SW2。您可能想要初始化ActivityViewController與圖像或UIActivityItemSource

+0

看到了,謝謝你的回答,但它不能幫助我。如果我不夠清楚,我很抱歉,但儘管這是我想要的(爲了分享),但我問的是如何特別分享一個動畫.gif文件?在Android中,我所要做的就是指定文件的路徑,說「這是一個圖像」,然後調用共享意圖。看起來在iOS中,您可以共享文本,網址或UIImage,也可以是靜態圖片,至少這是我見過的。 – FTLRalph 2014-10-17 11:47:44

0

Facebook和Twitter不支持UIActivityViewController,仍然在這個只使用有限的應用程序。現在這個代碼非常有用。

- (無效)uploadImageToTwitter {

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier: 
           ACAccountTypeIdentifierTwitter]; 

[account requestAccessToAccountsWithType:accountType options:nil 
           completion:^(BOOL granted, NSError *error) 
{ 
    if (granted == YES) 
    { 
     NSArray *arrayOfAccounts = [account 
            accountsWithAccountType:accountType]; 

     if ([arrayOfAccounts count] > 0) 
     { 
      ACAccount *twitterAccount = 
      [arrayOfAccounts lastObject]; 

//
// NSURL *捲起= [NSURL fileURLWithPath:NSTemporaryDirectory()]; // NSURL * fileURL = [furl URLByAppendingPathComponent:@「animation.gif」];

   NSData *imageData = [NSData dataWithContentsOfURL:_GIFURL]; 

       NSURL *requestURL = [NSURL URLWithString:@"https://upload.twitter.com/1.1/media/upload.json"]; 

      SLRequest *postRequest = [SLRequest 
             requestForServiceType:SLServiceTypeTwitter 
             requestMethod:SLRequestMethodPOST 
             URL:requestURL parameters:nil]; 

      postRequest.account = twitterAccount; 

      [postRequest addMultipartData:imageData 
            withName:@"media" 
             type:@"image/gif" 
            filename:@"test.gif"]; 

      [postRequest 
       performRequestWithHandler:^(NSData *responseData, 
              NSHTTPURLResponse *urlResponse, NSError *error) 
       { 


        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

        NSString *mediaID = [json objectForKey:@"media_id_string"]; 


        if (mediaID!=nil) { 


         NSURL *requestURL2 = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]; 
         NSDictionary *message2 = @{@"status": @"Here is the image", 
               @"media_ids": mediaID }; 

         SLRequest *postRequest2 = [SLRequest 
               requestForServiceType:SLServiceTypeTwitter 
               requestMethod:SLRequestMethodPOST 
               URL:requestURL2 parameters:message2]; 
         postRequest2.account = twitterAccount; 

         [postRequest2 
         performRequestWithHandler:^(NSData *responseData, 
                NSHTTPURLResponse *urlResponse, NSError *error) 
         { 
          // DONE!!! 

          UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Notification" message:@"Upload Twitter Account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
          [alert show]; 


         }]; 

        } 

       }]; 
     } 
    } 
}]; 

}