【发布时间】:2011-12-29 12:06:45
【问题描述】:
我有一个从 Web 服务返回的数组,其中包含字节值(非二进制格式,即 0-255)。这些代表缩略图。我想需要从这些值创建一个 NSData 对象。我该怎么做?
【问题讨论】:
我有一个从 Web 服务返回的数组,其中包含字节值(非二进制格式,即 0-255)。这些代表缩略图。我想需要从这些值创建一个 NSData 对象。我该怎么做?
【问题讨论】:
+ (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];
}
【讨论】: