我有一个图像,我想发送作为字节数组到服务器。我想知道如何将uiimage转换为字节数组?我不得不提前发参数作为如何将UIImage转换为字节数组或base64encoding?
<byteArrayIn>base64Binary</byteArrayIn>
到Web服务
感谢
我有一个图像,我想发送作为字节数组到服务器。我想知道如何将uiimage转换为字节数组?我不得不提前发参数作为如何将UIImage转换为字节数组或base64encoding?
<byteArrayIn>base64Binary</byteArrayIn>
到Web服务
感谢
UIImage *img = ...;
CGFloat quality = 0.85;
NSData *jpegdata = UIImageJPEGRepresentation(img,quality);
或
NSData *pngdata = UIImagePNGRepresentation(img);
下面是一个简单的功能,适用于iOS从UIImage的转换为字节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;
}
一旦你有字节数组数据,base64编码的算法很容易实现,你可以在wikipedia上读到它。也许有一个更简单的方法来base64编码,以及我不知道。 http://en.wikipedia.org/wiki/Base64
由于多种原因,这是效率低下的方式。 UIImagePNGRepresentation好得多。 –
但图像不作为JPEG数据保存在内存中。当你'CGContextDrawImage'时,你画一个BMP。 –
我只是想提供一个从UIImage转换为字节数组的方法。它将会变慢,但以防万一您想在将数据发送到服务器之前修改数据。 –