// // FUImageHelper.m // FULiveDemo // // Created by L on 2018/8/3. // Copyright © 2018年 L. All rights reserved. // #import "FUImageHelper.h" @implementation FUImageHelper + (void) convertUIImageToBitmapRGBA8:(UIImage *) image completionHandler:(void (^)(int32_t size, unsigned char * bits))completionHandler { CGImageRef imageRef = image.CGImage; // Create a bitmap context to draw the uiimage into CGContextRef context = [self newBitmapRGBA8ContextFromImage:imageRef]; if(!context) { return ; } int32_t width = (int32_t)CGImageGetWidth(imageRef); int32_t height = (int32_t)CGImageGetHeight(imageRef); CGRect rect = 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 unsigned char *bitmapData = (unsigned char *)CGBitmapContextGetData(context); // Copy the data and release the memory (return memory allocated with new) size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context); size_t bufferLength = bytesPerRow * height; unsigned char *newBitmap = NULL; union i32c{ int32_t i32v; unsigned char bytes[4]; }; union i32c cwidth; cwidth.i32v = width; union i32c cheight; cheight.i32v = height; if(bitmapData) { newBitmap = (unsigned char *)malloc(sizeof(unsigned char) * bytesPerRow * height + 8); newBitmap[0] =cwidth.bytes[0]; newBitmap[1] =cwidth.bytes[1]; newBitmap[2] =cwidth.bytes[2]; newBitmap[3] =cwidth.bytes[3]; newBitmap[4]=cheight.bytes[0]; newBitmap[5] =cheight.bytes[1]; newBitmap[6] =cheight.bytes[2]; newBitmap[7] =cheight.bytes[3]; if(newBitmap) { // Copy the data for(int i = 8; i < bufferLength+8; ++i) { newBitmap[i] = bitmapData[i - 8]; } } free(bitmapData); } else { NSLog(@"Error getting bitmap pixel data\n"); } CGContextRelease(context); completionHandler((size_t)bufferLength +8,newBitmap); } + (CGContextRef) newBitmapRGBA8ContextFromImage:(CGImageRef) image { CGContextRef context = NULL; CGColorSpaceRef colorSpace; uint32_t *bitmapData; size_t bitsPerPixel = 32; size_t bitsPerComponent = 8; size_t bytesPerPixel = bitsPerPixel / bitsPerComponent; size_t width = CGImageGetWidth(image); size_t height = CGImageGetHeight(image); size_t bytesPerRow = width * bytesPerPixel; size_t bufferLength = bytesPerRow * height; colorSpace = CGColorSpaceCreateDeviceRGB(); if(!colorSpace) { NSLog(@"Error allocating color space RGB\n"); return NULL; } // Allocate memory for image data bitmapData = (uint32_t *)malloc(bufferLength); if(!bitmapData) { NSLog(@"Error allocating memory for bitmap\n"); CGColorSpaceRelease(colorSpace); return NULL; } //Create bitmap context context = CGBitmapContextCreate(bitmapData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); // RGBA if(!context) { free(bitmapData); NSLog(@"Bitmap context not created"); } CGColorSpaceRelease(colorSpace); return context; } + (unsigned char *)getRGBAWithImage:(UIImage *)image { int RGBA = 4; CGImageRef imageRef = [image CGImage]; size_t width = CGImageGetWidth(imageRef); size_t height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = (unsigned char *) malloc(width * height * sizeof(unsigned char) * RGBA); NSUInteger bytesPerPixel = RGBA; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CFRelease(imageRef); CGColorSpaceRelease(colorSpace); CGContextRelease(context); return rawData; } + (unsigned char *)getRGBAWithImageName:(NSString *)imageName width:(int *)width height:(int *)height{ //获取图片文件路径 NSString * path = [[NSBundle mainBundle]pathForResource:imageName ofType:@"png"]; NSURL * url = [NSURL fileURLWithPath:path]; CGImageRef imageRef = NULL; CGImageSourceRef myImageSource; //通过文件路径创建CGImageSource对象 myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, NULL); //获取第一张图片 imageRef = CGImageSourceCreateImageAtIndex(myImageSource, 0, NULL); size_t width0 = CGImageGetWidth(imageRef); size_t height0 = CGImageGetHeight(imageRef); *width = (int)width0; *height = (int)height0; int RGBA = 4; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = (unsigned char *) malloc(width0 * height0 * sizeof(unsigned char) * RGBA); memset(rawData, 0, width0 * height0 * sizeof(unsigned char) * RGBA); NSUInteger bytesPerPixel = RGBA; NSUInteger bytesPerRow = bytesPerPixel * width0; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width0, height0, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, width0, height0), imageRef); CFRelease(imageRef); CGColorSpaceRelease(colorSpace); CGContextRelease(context); CFRelease(myImageSource); return rawData; } + (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer withWidth:(int) width withHeight:(int) height{ /* 一些到blend完,带上了素材alpha,导致保存效果不对,强行将alpha = 1.0 */ int length = height * width * 4; for (int i=0; i