2011-12-29 94 views
0

我有一個從包含字節值(非二進制格式,即0-255)的Web服務返回的數組。這些代表縮略圖。我想需要從這些值創建一個NSData對象。我怎樣才能做到這一點?將NSArray的字節值轉換爲NSData

回答

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]; 
}