2

sendResourceAtURL を使用してimagePickerController経由で選択した画像を送信しようとしています。メソッドdidFinishPickingMediaWithInfoでURL を取得します。

NSURL *refURL = [info objectForKey:UIImagePickerControllerReferenceURL];

でも電話したら

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

サポートされていないリソース タイプ エラーが常に表示されます。URL の構造が正しくないと思います。他に sth が必要です。バンドル内のローカル ファイルで URL を作成すると、正常に送信されます。何か案は?

ありがとう、ゾイス

4

1 に答える 1

4

Multipeer Group Chat サンプル アプリで提供されているコードを使用したところ、うまくいきました。最初にファイルをドキュメント ディレクトリに書き込んでから、その 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];
    }];
});
于 2014-01-12T00:26:20.827 に答える