char * to NSData to display in UIImageView
I am getting image data in form of char * (stored on sql server). I wa开发者_运维百科nt to convert those data to NSData to display image in UIImage view. char * data is a bytearray for source image. how can i convert char * to NSData to display to get an UIImage instance for displaying in UIImageView?
Thanks..
Given:
unsigned char *myImageData;
NSUInteger myImageDataLength;
Where myImageData
is the image in JPEG, PNG or some other UIImage supported format, use:
-(UIImage*)myImage {
return [UIImage imageWithData:[NSData dataWithBytes:myImageData length:myImageDataLength]];
}
If your char data is Base64 encoded, check out http://www.cocoadev.com/index.pl?BaseSixtyFour.
I've done this both with data from a web service and to bake-in an image. For example:
// Note hex = ".PNG..", the start of the PNG header.
static unsigned char myImageData[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a,... };
The length is the image file size, and the hex values can be created from the image file with:
hexdump -e ' 16/1 "0x%02x, " "\n"' MyImage.png
精彩评论