1

我正在获取 char * 形式的图像数据(存储在 sql server 上)。我想将这些数据转换为 NSData 以在 UIImage 视图中显示图像。char * data 是源图像的字节数组。如何将 char * 转换为 NSData 以显示以获取 UIImage 实例以在 UIImageView 中显示?

谢谢..

4

1 回答 1

3

鉴于:

unsigned char *myImageData;
NSUInteger myImageDataLength;

myImageDataJPEG、PNG 或其他 UIImage 支持格式的图像在哪里,请使用:

-(UIImage*)myImage {
    return [UIImage imageWithData:[NSData dataWithBytes:myImageData length:myImageDataLength]];
}

如果您的 char 数据是 Base64 编码的,请查看http://www.cocoadev.com/index.pl?BaseSixtyFour

我已经使用来自 Web 服务的数据和烘焙图像来完成此操作。例如:

// Note hex = ".PNG..", the start of the PNG header.
static unsigned char myImageData[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a,... };  

长度是图像文件的大小,可以从图像文件创建十六进制值:

hexdump -e ' 16/1 "0x%02x, " "\n"' MyImage.png
于 2009-12-18T07:08:39.317 回答