iOS实现图像特效相关
来源:互联网 发布:天宇水利预算软件 编辑:程序博客网 时间:2024/06/06 02:32
通过慕课网学习了一些图像处理相关的知识点,高大上的特效难点在于算法上,图形处理是小case,好了,talk is cheap, show me the code!
首先需要将 UIImage 转化成 CGImage 数据流:
- (unsigned char*)convertUIImageToData:(UIImage *)image { CGImageRef imageref = [image CGImage]; CGSize image_size = image.size; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); void *data = malloc(image_size.width*image_size.height*4); CGContextRef context = CGBitmapContextCreate(data, image_size.width, image_size.height, 8, 4*image_size.width, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, image_size.width, image_size.height), imageref); CGColorSpaceRelease(colorSpace); CGContextRelease(context); return (unsigned char*)data;}
灰度处理:
- (unsigned char*)imageGrayWithData:(unsigned char*)imageData width:(CGFloat)width height:(CGFloat)height { unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4); memset(resultData, 0, width*height*sizeof(unsigned char)*4); for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { unsigned int imageIndex = h*width+w; unsigned char bitmapRed = *(imageData+imageIndex*4); unsigned char bitmapGreen = *(imageData+imageIndex*4+1); unsigned char bitmapBlue = *(imageData+imageIndex*4+2); int bitMap = bitmapRed*77/255+bitmapGreen*151/255+bitmapBlue*88/255; unsigned char newBitMap = (bitMap>255)?255:bitMap; memset(resultData+imageIndex*4, newBitMap, 1); memset(resultData+imageIndex*4+1, newBitMap, 1); memset(resultData+imageIndex*4+2, newBitMap, 1); } } return resultData;}
反色处理:
- (unsigned char*)imageReColorWithData:(unsigned char*)imageData width:(CGFloat)width height:(CGFloat)height { unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4); memset(resultData, 0, width*height*sizeof(unsigned char)*4); for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { unsigned int imageIndex = h*width+w; unsigned char bitmapRed = *(imageData+imageIndex*4); unsigned char bitmapGreen = *(imageData+imageIndex*4+1); unsigned char bitmapBlue = *(imageData+imageIndex*4+2); unsigned char bitMapRedNew = 255-bitmapRed; unsigned char bitMapGreenNew = 255-bitmapGreen; unsigned char bitMapBlueNew = 255-bitmapBlue; memset(resultData+imageIndex*4, bitMapRedNew, 1); memset(resultData+imageIndex*4+1, bitMapGreenNew, 1); memset(resultData+imageIndex*4+2, bitMapBlueNew, 1); } } return resultData;}
简单的高亮美白处理:
- (unsigned char*)imageHighlightWithData:(unsigned char*)imageData width:(CGFloat)width height:(CGFloat)height { unsigned char *resultData = malloc(width*height*sizeof(unsigned char)*4); memset(resultData, 0, width*height*sizeof(unsigned char)*4); NSArray *colorArrayBase = @[@"55",@"110",@"155",@"185",@"220",@"240",@"250",@"255"]; NSMutableArray *colorArray = [NSMutableArray new]; int beforNum = 0; for (int i = 0; i < 8; i++) { NSString *numStr = [colorArrayBase objectAtIndex:i]; int num = numStr.intValue; float step = 0; if (i==0) { step = num/32.0; beforNum = num; } else { step = (num-beforNum)/32.0; } for (int j = 0; j < 32; j++) { int newNum = 0; if (i==0) { newNum = (int)j*step; } else { newNum = (int)(beforNum+j*step); } NSString *newNumStr = [NSString stringWithFormat:@"%d",newNum]; [colorArray addObject:newNumStr]; } beforNum = num; } for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { unsigned int imageIndex = h*width+w; unsigned char bitmapRed = *(imageData+imageIndex*4); unsigned char bitmapGreen = *(imageData+imageIndex*4+1); unsigned char bitmapBlue = *(imageData+imageIndex*4+2); NSString *redStr = [colorArray objectAtIndex:bitmapRed]; NSString *greenStr = [colorArray objectAtIndex:bitmapGreen]; NSString *blueStr = [colorArray objectAtIndex:bitmapBlue]; unsigned char bitMapRedNew = redStr.intValue; unsigned char bitMapGreenNew = greenStr.intValue; unsigned char bitMapBlueNew = blueStr.intValue; memset(resultData+imageIndex*4, bitMapRedNew, 1); memset(resultData+imageIndex*4+1, bitMapGreenNew, 1); memset(resultData+imageIndex*4+2, bitMapBlueNew, 1); } } return resultData;}
将数据流转化回 UIImage:
- (UIImage *)convertDataToUIImage:(unsigned char*)imageData image:(UIImage *)imageSource { CGFloat width = imageSource.size.width; CGFloat height = imageSource.size.height; NSInteger dataLength = width*height*4; CGDataProviderRef provide = CGDataProviderCreateWithData(NULL, imageData, dataLength, NULL); int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4*width; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderIntent = kCGRenderingIntentDefault; CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provide, NULL, NO, renderIntent); UIImage *imageNew = [UIImage imageWithCGImage:imageRef]; CFRelease(imageRef); CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provide); return imageNew;}
阅读全文
0 0
- iOS实现图像特效相关
- GDI+实现各种图像特效
- ios 图像开发相关
- iOS OurChoice折叠特效实现
- 用JavaScript实现的十个图像特效
- GDI实现图像的简单显示特效
- 实现图像特效之浮雕与雕刻
- 图像特效
- 图像滤镜艺术---PS图像转手绘特效实现方案
- 【IOS】实现一种书本的展示特效
- 【IOS】实现一种书本的展示特效
- iOS图片滤镜特效的实现
- IOS中各种动画特效的实现
- iOS的几个特效实现思路
- 图像特效之水彩特效
- 图像特效之灯光特效
- 图像特效之挤压特效
- 图像特效之波浪特效
- 拉格朗日乘子法和对偶问题
- 信号屏蔽pending
- phpMyadmin 安装错误及解决方法
- js中将某个value前面补0然后整个字符串还是12位
- maven+ssm例子
- iOS实现图像特效相关
- 回溯法——0-1背包问题
- 启动myeclipse的时候弹出来workspace in use or cannot be created, choose a different one
- hadoop2.6.0学习
- Hdu4135 Co-prime
- SSL 1333_地鼠的困境_匹配
- 阿里云Nginx服务器配置301重定向
- saas平台产品使用合同(模板)
- PCL中读取pcd点云数据的两种方法