2013-11-03 13 views
2

我想發送一個圖像,我選擇通過imagePickerController使用sendResourceAtURL。 我獲得方法didFinishPickingMediaWithInfo URL作爲使用sendResourceAtURL給出「不支持的資源類型」

NSURL *refURL = [info objectForKey:UIImagePickerControllerReferenceURL]; 

但是,當我打電話

[self.mySession sendResourceAtURL:refURL withName:@"test" toPeer:peerid withCompletionHandler:^(NSError *error){ 
       if (error).... 

我總是得到了不支持的資源類型錯誤。 我相信url的構造是不正確的,我需要別人。 當我創建一個包含我的包中的本地文件的url時,它會被正常傳輸。 任何想法?

感謝, Zois

+0

您確認'refURL'實際上是一個指向有效資源的NSURL對象嗎? – ChrisH

+0

是的,它是由圖像選擇器瀏覽器選取的圖像。如果我登錄它,我會得到_assets-library://asset/asset.PNG?id = EE8FE14C-63FF-4D8B-BB12-3E97506C2188&ext = PNG_。所以正如我提到的,我必須以不同的方式構建該對象的URL。例如,通過使用ALAssetsLibrary,我可以獲取圖像的名稱。 – user2950891

+0

您可以粘貼將URL分配給'refURL'變量的整個方法嗎? – Yazid

回答

4

我曾經在Multipeer集團提供聊天的示例應用程序的代碼和工作。您首先必須將文件寫入文檔目錄,然後發送該URL。

// Don't block the UI when writing the image to documents 
dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
    // We only handle a still image 
    UIImage *imageToSave = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; 

    // Save the new image to the documents directory 
    NSData *pngData = UIImageJPEGRepresentation(imageToSave, 1.0); 

    // Create a unique file name 
    NSDateFormatter *inFormat = [NSDateFormatter new]; 
    [inFormat setDateFormat:@"yyMMdd-HHmmss"]; 
    NSString *imageName = [NSString stringWithFormat:@"image-%@.JPG", [inFormat stringFromDate:[NSDate date]]]; 
    // Create a file path to our documents directory 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName]; 
    [pngData writeToFile:filePath atomically:YES]; // Write the file 
    // Get a URL for this file resource 
    NSURL *imageUrl = [NSURL fileURLWithPath:filePath]; 

    MCPeerID *peer = self.session.connectedPeers[0]; 
    [self.session sendResourceAtURL:imageUrl withName:imageName toPeer:peer withCompletionHandler:^(NSError *error) { 
     if (error) { 
      NSLog(@"Failed to send picture to %@, %@", peer.displayName, error.localizedDescription); 
      return; 
     } 
     NSLog(@"Sent picture to %@", peer.displayName); 
     //Clean up the temp file 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     [fileManager removeItemAtURL:imageUrl error:nil]; 
    }]; 
}); 
+0

有沒有可用的列表,我們可以看到所有支持的資源類型?我試着發送一個PNG文件,並得到了不受支持的資源類型錯誤。 – Anshul

+0

不確定。 file://是爲我工作的那個。您從照片庫中獲得的圖像具有不同的類型。 http://也可以工作。 –

+0

是的,讓它工作。我的錯!!實際上,我們可以傳輸任何類型的資源。您也可以創建自定義文件擴展名。 – Anshul

相關問題