0

画像があり、Bluetooth 経由で別のデバイスに送信する必要があります。データは次のとおりです。

NSData *Recording = [NSData dataWithContentsOfFile:myFilePath];
NSString* str = [NSString stringWithFormat:@"%@.ext", button.titleLabel.text];
myDictionary = [[NSMutableDictionary alloc] init];
                    [myDictionary setObject:str forKey:@"fileName"];
                    [myDictionary setObject:@"Recording" forKey:@"fileType"];
                    [myDictionary setObject:Recording forKey:@"FileData"];


NSData* myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

次のメソッドを使用して、myData または myDictionary を送信する必要があります。

progress = [session sendResourceAtURL:anUrl withName:[imageUrl lastPathComponent] toPeer:peerID withCompletionHandler:^(NSError *error) {
        // Implement this block to know when the sending resource transfer completes and if there is an error.
        if (error) {
            NSLog(@"Send resource to peer [%@] completed with Error [%@]", peerID.displayName, error);
        }
        else {
            // Create an image transcript for this received image resource

        }
    }];

ご覧のとおり、url でリソースを送信する必要があり、レシーバーは「url で」リソースを受信し、その URL から NSMutabledictionary を取得する必要があります。基本的に、この myDictionary を送信する必要がありますが、URL がどのように機能するかを理解するのに問題があります。使用する必要がある URL とその作成方法を誰かに説明してもらえますか?

4

1 に答える 1

1

あなたが何を望んでいるのかよくわかりませんが、あなたは乗り換え地点で立ち往生しているようです。

すでにすべてを NSData オブジェクトに変換しているため、送信者側では次のように呼び出す必要があります。

// Assume peerID is correct and data has been prepared
NSError *error;
if ( ![session sendData:data
                toPeers:@[peerID]
               withMode:MCSessionSendDataReliable
                  error:&error] )
    NSLog(@"ERROR - Unable to queue message for delivery - %@",[error localizedDescription]);

受信者側では、MCSessionDelegate が誰であれ、次の方法で呼び出されます。

- (void)session:(MCSession *)session
        didReceiveData:(NSData *)data
              fromPeer:(MCPeerID *)peerID;

NSData オブジェクトを「アーカイブ解除」して何かを行う可能性が高い場所。

私も観察を追加しましょう...

あなたの例から、ファイルのコンテンツ全体を辞書に詰め込み、アーカイブして送信しているように見えます。それはおそらく実行可能ですが、お勧めできません。

より良いアプローチは、ファイルを別のリソースとして送信することです。たとえば、送信者のこのサンプルを見てください。

// Assume peerID and filePath are both valid and correct 
NSURL *fileURL = [NSURL URLWithString:filePath];
NSString *fileName = [fileURL lastPathComponent];
NSError *error;
[session sendResourceAtURL:resourceURL
                  withName:fileName
                    toPeer:peerID
     withCompletionHandler:^(NSError *error) {
         if (error)
             NSLog(@"Sending to [%@] failed: %@",peerID.displayName, error);
     }];

受信側の MCSessionDelegate は、転送開始時に次の両方を受け取ります。

- (void)session:(MCSession *)session 
        didStartReceivingResourceWithName:(NSString *)resourceName
                                 fromPeer:(MCPeerID *)peerID
                             withProgress:(NSProgress *)progress;

...そして、これは転送が終了したとき:

- (void)session:(MCSession *)session 
        didFinishReceivingResourceWithName:(NSString *)resourceName
                                  fromPeer:(MCPeerID *)peerID
                                     atURL:(NSURL *)temporaryURL
                                 withError:(NSError *)error;
于 2013-11-20T11:02:53.340 に答える