最近在做圖片上傳七牛云, ?但是發現uiimage 轉成nsdata 竟然返回nil ?
原因分析: ?
1:項目中集成了opencv 其中圖片識別是用c++代碼完成 mat 轉換成 image 發生錯誤,
2:c++代碼部分 ?采用了國標編碼 ?不是常用的utf-8 編碼格式 ?可能會跟這個有關系
第一種嘗試是
NSData*data =UIImageJPEGRepresentation(imageCopy,0.6);
返回結果為nil
第二種嘗試:
CGDataProviderRef provider = CGImageGetDataProvider(image.CGImage);
NSData *datae = (id)CFBridgingRelease(CGDataProviderCopyData(provider));
nsdate數據是有了 但是上傳七牛云后 圖片拿下來是無法識別的 ?一臉懵逼
第三種嘗試 (也是成功的一種)? 直接貼代碼? imageHelper 創建一個類 圖片一個新的Bitmap 結果成功,
//ImageHelper.m
//56Class
//
//Created by Captain on 17/6/19.
//Copyright ? 2017年Captain. All rights reserved.
//
#import"ImageHelper.h"
@implementationImageHelper
+ (unsignedchar*) convertUIImageToBitmapRGBA8:(UIImage*) image {
CGImageRefimageRef = image.CGImage;
// Create a bitmap context to draw the uiimage into
CGContextRefcontext = [selfnewBitmapRGBA8ContextFromImage:imageRef];
if(!context) {
returnNULL;
}
size_twidth =CGImageGetWidth(imageRef);
size_theight =CGImageGetHeight(imageRef);
CGRectrect =CGRectMake(0,0, width, height);
// Draw image into the context to get the raw image data
CGContextDrawImage(context, rect, imageRef);
// Get a pointer to the data
unsignedchar*bitmapData = (unsignedchar*)CGBitmapContextGetData(context);
// Copy the data and release the memory (return memory allocated with new)
size_tbytesPerRow =CGBitmapContextGetBytesPerRow(context);
size_tbufferLength = bytesPerRow * height;
unsignedchar*newBitmap =NULL;
if(bitmapData) {
newBitmap = (unsignedchar*)malloc(sizeof(unsignedchar) * bytesPerRow * height);
if(newBitmap) {// Copy the data
for(inti =0; i < bufferLength; ++i) {
newBitmap[i] = bitmapData[i];
}
}
free(bitmapData);
}else{
NSLog(@"Error getting bitmap pixel data\n");
}
CGContextRelease(context);
returnnewBitmap;
}
+ (CGContextRef) newBitmapRGBA8ContextFromImage:(CGImageRef) image {
CGContextRefcontext =NULL;
CGColorSpaceRefcolorSpace;
uint32_t*bitmapData;
size_tbitsPerPixel =32;
size_tbitsPerComponent =8;
size_tbytesPerPixel = bitsPerPixel / bitsPerComponent;
size_twidth =CGImageGetWidth(image);
size_theight =CGImageGetHeight(image);
size_tbytesPerRow = width * bytesPerPixel;
size_tbufferLength = bytesPerRow * height;
colorSpace =CGColorSpaceCreateDeviceRGB();
if(!colorSpace) {
NSLog(@"Error allocating color space RGB\n");
returnNULL;
}
// Allocate memory for image data
bitmapData = (uint32_t*)malloc(bufferLength);
if(!bitmapData) {
NSLog(@"Error allocating memory for bitmap\n");
CGColorSpaceRelease(colorSpace);
returnNULL;
}
//Create bitmap context
context =CGBitmapContextCreate(bitmapData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);// RGBA
if(!context) {
free(bitmapData);
NSLog(@"Bitmap context not created");
}
CGColorSpaceRelease(colorSpace);
returncontext;
}
+ (UIImage*) convertBitmapRGBA8ToUIImage:(unsignedchar*) buffer
withWidth:(int) width
withHeight:(int) height {
size_tbufferLength = width * height *4;
CGDataProviderRefprovider =CGDataProviderCreateWithData(NULL, buffer, bufferLength,NULL);
size_tbitsPerComponent =8;
size_tbitsPerPixel =32;
size_tbytesPerRow =4* width;
CGColorSpaceRefcolorSpaceRef =CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef ==NULL) {
NSLog(@"Error allocating color space");
CGDataProviderRelease(provider);
returnnil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider,// data provider
NULL,// decode
YES,// should interpolate
renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
if(pixels ==NULL) {
NSLog(@"Error: Memory not allocated for bitmap");
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
returnnil;
}
CGContextRef context = CGBitmapContextCreate(pixels,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
kCGImageAlphaPremultipliedLast);
if(context ==NULL) {
NSLog(@"Error context not created");
free(pixels);
}
UIImage *image =nil;
if(context) {
CGContextDrawImage(context, CGRectMake(0.0f,0.0f, width, height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
floatscale = [[UIScreen mainScreen] scale];
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
}else{
image = [UIImage imageWithCGImage:imageRef];
}
CGImageRelease(imageRef);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);
if(pixels) {
free(pixels);
}
returnimage;
}
@end
使用方法如下:
unsignedchar*bitmap = [ImageHelperconvertUIImageToBitmapRGBA8:image];
int width = image.size.width;
int height = image.size.height;
UIImage*imageCopy = [ImageHelper convertBitmapRGBA8ToUIImage:bitmapwithWidth:widthwithHeight:height];
NSData*data =UIImageJPEGRepresentation(imageCopy,0.6);
這樣nsdata就有數據就可以完成圖片上傳七牛云, 希望可以幫到大家 ?我也這記錄一下我的錯誤