我有一個圖像,我想發送作爲字節數組到服務器。我想知道如何將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轉換爲字節數組的方法。它將會變慢,但以防萬一您想在將數據發送到服務器之前修改數據。 –