【问题标题】:Convert an NSArray of byte values into an NSData将字节值的 NSArray 转换为 NSData
【发布时间】:2011-12-29 12:06:45
【问题描述】:

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

【问题讨论】:

    标签: nsarray nsdata


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2012-05-28
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多