iOS 上傳七牛云失敗 uiimage 轉(zhuǎn)nsdata 結(jié)果返回nil 快來看這里完美解決

最近在做圖片上傳七牛云, ?但是發(fā)現(xiàn)uiimage 轉(zhuǎn)成nsdata 竟然返回nil ?

原因分析: ?

1:項目中集成了opencv 其中圖片識別是用c++代碼完成 mat 轉(zhuǎn)換成 image 發(fā)生錯誤,

2:c++代碼部分 ?采用了國標編碼 ?不是常用的utf-8 編碼格式 ?可能會跟這個有關(guān)系

第一種嘗試是

NSData*data =UIImageJPEGRepresentation(imageCopy,0.6);

返回結(jié)果為nil

第二種嘗試:

CGDataProviderRef provider = CGImageGetDataProvider(image.CGImage);

NSData *datae = (id)CFBridgingRelease(CGDataProviderCopyData(provider));

nsdate數(shù)據(jù)是有了 但是上傳七牛云后 圖片拿下來是無法識別的 ?一臉懵逼

第三種嘗試 (也是成功的一種)? 直接貼代碼? imageHelper 創(chuàng)建一個類 圖片一個新的Bitmap 結(jié)果成功,


//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就有數(shù)據(jù)就可以完成圖片上傳七牛云, 希望可以幫到大家 ?我也這記錄一下我的錯誤

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容