0

我通过 Bonjour 发送了一个 NSMutableArray

- (void)sendArtwork {
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    NSData *imageData = [self PNGRepresentationOfImage:[bHelp getArtwork:[playerPref selectedRow]]];
    [dataArray addObject:songString];
    [dataArray addObject:artistString];
    [dataArray addObject:imageData];
    NSData *finalData = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
    [self.server sendData:finalData error:nil];
}

- (NSData *)PNGRepresentationOfImage:(NSImage *)image {
    [image lockFocus];
    NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
    [image unlockFocus];

    return [bitmapRep representationUsingType:NSJPEGFileType properties:nil];
}

然后接收并取消归档

NSMutableArray *dataArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

此代码在模拟器中完美运行。但是一旦我在我的设备上运行,解压器就会抛出这个错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'

如果我不发送imageData可变数组,我不会在设备上收到错误消息。我正在使用 Bill Dudney 的 iOS 示例和 Brad Larson 的Mac 示例

4

1 回答 1

0

根据我对 Bonjour 的了解,您可以发送任意数据,但是当您阅读它时,您不会直接获得 NSData 对象。我会把这个问题作为参考:

Cocoa Touch Bonjour如何处理NSNetService地址和uint8_t

因此,当您读取数据时,您将构建一个 NSData 对象,如下所示:

NSData* data = [NSData dataWithBytes: buffer length: actuallyRead];
//                              ^                ^
//                            Data read       Number of bytes read
于 2013-02-11T22:38:48.163 回答