iOS 上传七牛云失败 uiimage 转nsdata 结果返回nil 快来看这里完美解决

最近在做图片上传七牛云,  但是发现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就有数据就可以完成图片上传七牛云, 希望可以帮到大家  我也这记录一下我的错误

你可能感兴趣的:(iOS 上传七牛云失败 uiimage 转nsdata 结果返回nil 快来看这里完美解决)