使用MultiPeer框架将NSData发送给对等方

时间:2015-09-20 21:35:15

标签: ios swift

我已成功将multiPeer框架实现到我的应用程序中,并可以轻松地将图像和字符串传递给其他设备。我的问题是当我尝试将NSArray传递给NSData时。当调用multipeer didReceiveData数据函数时,我总是遇到以下崩溃:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [NSKeyedUnarchiver initForReadingWithData:]:难以理解的存档

以下是我发送数据的方式:

   var myNSData: NSData = NSKeyedArchiver.archivedDataWithRootObject(arrayOfNumbers)


        var error : NSError?

        self.session.sendData(myNSData, toPeers: self.session.connectedPeers,
            withMode: MCSessionSendDataMode.Reliable, error: &error)

        if error != nil {
            print("Error sending data: \(error?.localizedDescription)")
        }

这就是我试图收集数据的方式:

    func session(session: MCSession!, didReceiveData data: NSData!,
    fromPeer peerID: MCPeerID!)  {
        // Called when a peer sends an NSData to us

        // This needs to run on the main queue
        dispatch_async(dispatch_get_main_queue()) {

  // can't convert data back NSArray without crash 
      var receivedArray:NSArray = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! NSArray

1 个答案:

答案 0 :(得分:0)

确实,您还没有提供确定问题所需的所有相关代码;但是,我可以这样说:(1)关于使用数组的方法毫无意义。如果您愿意,我可以提供完美的图像广播代码;并且,(2)尝试使用NSValue对数组进行编码,然后归档NSValue对象。

确实,您还没有提供确定问题所需的所有相关代码;但是,我可以这样说:(1)关于使用数组的方法毫无意义。我已在下面为您提供了完美的图像广播代码;并且,(2)尝试使用NSValue对数组进行编码,然后归档NSValue对象。

这是最好的方法:

在iOS设备上发送图像数据:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);


    UIImage *image = [[UIImage alloc] initWithCGImage:newImage scale:1 orientation:UIImageOrientationUp];
    CGImageRelease(newImage);
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

    if (image) {
        NSData *data = UIImageJPEGRepresentation(image, 0.7);
        NSError *err;
        [((ViewController *)self.parentViewController).session sendData:data toPeers:((ViewController *)self.parentViewController).session.connectedPeers withMode:MCSessionSendDataReliable error:&err];
    }
}

在接收图像数据的iOS设备上:

typedef struct {
    size_t length;
    void *data;
} ImageCacheDataStruct;

- (void)session:(nonnull MCSession *)session didReceiveData:(nonnull NSData *)data fromPeer:(nonnull MCPeerID *)peerID
{
  dispatch_async(self.imageCacheDataQueue, ^{
        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
        const void *dataBuffer = [data bytes];
        size_t dataLength = [data length];
        ImageCacheDataStruct *imageCacheDataStruct = calloc(1, sizeof(imageCacheDataStruct));
        imageCacheDataStruct->data = (void*)dataBuffer;
        imageCacheDataStruct->length = dataLength;

        __block const void * kMyKey;
        dispatch_queue_set_specific(self.imageDisplayQueue, &kMyKey, (void *)imageCacheDataStruct, NULL);

        dispatch_sync(self.imageDisplayQueue, ^{
            ImageCacheDataStruct *imageCacheDataStruct = calloc(1, sizeof(imageCacheDataStruct));
            imageCacheDataStruct = dispatch_queue_get_specific(self.imageDisplayQueue, &kMyKey);
            const void *dataBytes = imageCacheDataStruct->data;
            size_t length = imageCacheDataStruct->length;
            NSData *imageData = [NSData dataWithBytes:dataBytes length:length];
            UIImage *image = [UIImage imageWithData:imageData];
            if (image) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [((ViewerViewController *)self.childViewControllers.lastObject).view.layer setContents:(__bridge id)image.CGImage];
                    dispatch_semaphore_signal(self.semaphore);
                });
            }
        });
    });
}

信号量和单独的GCD队列的原因很简单:您希望帧以相等的时间间隔显示。否则,视频有时会慢慢放慢速度,然后加速到正常速度以便赶上。我的方案确保每个帧以相同的速度一个接一个播放,无论网络带宽瓶颈如何。