无法通过Bonjour发送位图

时间:2013-02-11 21:58:29

标签: objective-c nsmutablearray nskeyedarchiver

我通过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];

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

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

如果我没有在可变数组中发送imageData,我就不会在设备上收到错误。我正在使用Bill Dudney的iOS examaple和Brad Larson的Mac example

1 个答案:

答案 0 :(得分:0)

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

Cocoa Touch Bonjour how to deal with NSNetService addresses and uint8_t

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

NSData* data = [NSData dataWithBytes: buffer length: actuallyRead];
//                              ^                ^
//                            Data read       Number of bytes read