开发者

How to convert UIImage to byte array or base64encoding?

I have an image which i want to send as byte array to a server .I want to know how to convert uiimage to byte array ?开发者_开发百科 I have to send parameter as

<byteArrayIn>base64Binary</byteArrayIn> to the web service

Thanks in advance


UIImage *img = ...;
CGFloat quality = 0.85;
NSData *jpegdata = UIImageJPEGRepresentation(img,quality);

or

NSData *pngdata = UIImagePNGRepresentation(img);


Here is a simple function for iOS to convert from UIImage to byte array -->

+ (unsigned char*)UIImageToByteArray:(UIImage*)image; {

    unsigned char *imageData = (unsigned char*)(malloc( 4*image.size.width*image.size.height));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGImageRef imageRef = [image CGImage];
    CGContextRef bitmap = CGBitmapContextCreate( imageData,
                                                image.size.width,
                                                image.size.height,
                                                8,
                                                image.size.width*4,
                                                colorSpace,
                                                kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage( bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);

    CGContextRelease( bitmap);
    CGImageRelease( imageRef);
    CGColorSpaceRelease( colorSpace);

    return imageData;
}

once you have the byte array data, the algorithm for base64 encoding is easy to implement and you can read up on it on wikipedia. There may be an easier way to base64 encode it as well I am not sure. http://en.wikipedia.org/wiki/Base64

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜