透明图绘制背景(证件照换底色)
致谢:感谢宝锋同学对方法一提供的思路;方法二则是完全是宝锋同学提供的代码。
方法一:
- (void)changeBgColorWithColorCode:(NSString *)colorCode { CGImageRef cgimage = [self.alphaImage CGImage]; size_t width = CGImageGetWidth(cgimage); // 图片宽度 size_t height = CGImageGetHeight(cgimage); // 图片高度 unsigned char *data = calloc(width * height * 4, sizeof(unsigned char)); // 取图片首地址 size_t bitsPerComponent = 8; // r g b a 每个component bits数目 size_t bytesPerRow = width * 4; // 一张图片每行字节数目 (每个像素点包含r g b a 四个字节) CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间 CGContextRef context = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); UIColor *fillColor; if ([colorCode isEqualToString:@"white"]) {
//白色 fillColor = [UIColor whiteColor]; CGContextSetFillColorWithColor(context, fillColor.CGColor); CGContextFillRect(context, CGRectMake(0, 0, width, height)); CGContextFillPath(context); } else if ([colorCode isEqualToString:@"blue"]) {
//蓝色 fillColor = [UIColor colorWithHex:0x3C90E8]; CGContextSetFillColorWithColor(context, fillColor.CGColor); CGContextFillRect(context, CGRectMake(0, 0, width, height)); CGContextFillPath(context); } else if ([colorCode isEqualToString:@"red"]) {
//红色 fillColor = [UIColor colorWithHex:0xF00001]; CGContextSetFillColorWithColor(context, fillColor.CGColor); CGContextFillRect(context, CGRectMake(0, 0, width, height)); CGContextFillPath(context); } else if ([colorCode isEqualToString:@"blue_gradual"]) {
//蓝色渐变 CGColorRef startColor = [UIColor colorWithRed:60/255.f green:144/255.f blue:232/255.f alpha:1].CGColor; CGColorRef endColor = [UIColor whiteColor].CGColor; NSArray *colors = @[(__bridge id)startColor,(__bridge id)endColor]; CGFloat locations[] = { 0.0, 1.0 }; CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef) colors, locations); // CGFloat components[] = { // 60/255.f,144/255.f,232/255.f,1, // 1,1,1,1 // }; // CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locations, 2); CGContextDrawLinearGradient(context, gradient, CGPointMake(width, height), CGPointMake(0, 0), 0); } else if ([colorCode isEqualToString:@"red_gradual"]) {
//红色渐变 CGColorRef startColor = [UIColor colorWithRed:240/255.f green:0/255.f blue:1/255.f alpha:1].CGColor; CGColorRef endColor = [UIColor whiteColor].CGColor; NSArray *colors = @[(__bridge id)startColor,(__bridge id)endColor]; CGFloat locations[] = { 0.0, 1.0 }; CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef) colors, locations); CGContextDrawLinearGradient(context, gradient, CGPointMake(width, height), CGPointMake(0, 0), 0); } else if ([colorCode isEqualToString:@"gray_gradual"]) {
//灰色渐变 CGColorRef startColor = [UIColor colorWithRed:157/255.f green:157/255.f blue:157/255.f alpha:1].CGColor; CGColorRef endColor = [UIColor whiteColor].CGColor; NSArray *colors = @[(__bridge id)startColor,(__bridge id)endColor]; CGFloat locations[] = { 0.0, 1.0 }; CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef) colors, locations); CGContextDrawLinearGradient(context, gradient, CGPointMake(width, height), CGPointMake(0, 0), 0); } CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgimage); cgimage = CGBitmapContextCreateImage(context); self.singleImage = [UIImage imageWithCGImage:cgimage]; self.printImage = [self createPrintPhotoWithImage:self.singleImage]; NSData *data1 = UIImagePNGRepresentation(self.singleImage); [data1 writeToFile:[NSString stringWithFormat:@"证件照/%ld.png",time(NULL)] atomically:YES];
}
方法二:
int main(int argc, char * argv[]) { @autoreleasepool { UIImage *ig = [UIImage imageWithContentsOfFile:@"/Users/friday/Documents/MyGitHub/ImgMating/drawimg/ef038050d196e37752e2f57e2c042acc.png"]; CGImageRef cgImg = ig.CGImage; CGDataProviderRef dataRef = CGImageGetDataProvider(cgImg); CFDataRef data = CGDataProviderCopyData(dataRef); CGDataProviderRelease(dataRef); NSData * data1 = (NSData *)CFBridgingRelease(data); Byte *bitmapData = data1.bytes; int width = CGImageGetWidth(cgImg); int height = CGImageGetHeight(cgImg); int bitsPerComponent = CGImageGetBitsPerComponent(cgImg); int bitsPerPixel = CGImageGetBitsPerPixel(cgImg); int bytesPerRow = CGImageGetBytesPerRow(cgImg); CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgImg); CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data1); CGFloat *decode = CGImageGetDecode(cgImg); bool shouldInterpolate= CGImageGetShouldInterpolate(cgImg); CGColorRenderingIntent intent = CGImageGetRenderingIntent(cgImg); int dataLength = height * bytesPerRow; unsigned char *pByte = (unsigned char *)bitmapData; // Result = subview * subview.alpha + superView *superView.alpha * (1 - subview.alhpa) float superViewAlpha = 1.0; int superView = 0xff000000; int cutValue = 0xffffff00; int resultCutValue = 0x000000ff; // 渐变色 Byte red = 0xff; Byte red1 = 0x00; Byte green = 0x00; Byte green1 = 0xff; Byte blue = 0x00; Byte blue1 = 0x00; float redPer = (red1-red)/(float)height; float greenPer = (green1-green)/(float)height; float bluePer = (blue1-blue)/(float)height; for (int i = 0 ; i< dataLength; i+=4) { int row = i/bytesPerRow; float subviewAlpha = pByte[i+3]/255.0; // alpha; float cuRed = row*redPer+red; float cuGreen = row*greenPer+green; float cuBlue = row*bluePer+blue; pByte[i] = pByte[i]*subviewAlpha + cuRed*superViewAlpha*(1-subviewAlpha);//red pByte[i+1] = pByte[i+1]*subviewAlpha+ cuGreen*superViewAlpha*(1-subviewAlpha);// green pByte[i+2] = pByte[i+2]*subviewAlpha + cuBlue*superViewAlpha*(1-subviewAlpha);// blue pByte[i+3] = 255; } CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpaceCreateDeviceRGB(), bitmapInfo, provider, decode, shouldInterpolate, intent); UIImage *svImg = [[UIImage alloc]initWithCGImage:newImg]; NSData *data2 = UIImagePNGRepresentation(svImg); [data2 writeToFile:@"证件照.png" atomically:YES]; return 1; } }