views:

936

answers:

1

I am getting image data in form of char * (stored on sql server). I want 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..

+2  A: 

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
John Franklin