将NSArray的字节值转换为NSData

时间:2011-12-29 12:06:46

标签: nsarray nsdata

我有一个从包含字节值的Web服务返回的数组(非二进制格式,即0-255)。这些代表缩略图。我想要从这些值创建一个NSData对象。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length是你的朋友:

void bytesToNSDataExample() {
    unsigned char bytes[] = {'h', 'e', 'l', 'l', 'o'};
    NSData *data = [NSData dataWithBytes:bytes length:5];

    NSString *string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
    NSLog(@"%@", string);

    // if data was a image like in your case, you would probably do something like this:
    NSImageRep *imgRep = [NSBitmapImageRep imageRepWithData:data];
}