利用CoreGraphics
库制作滤镜,效果如下:
代码
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"IMG_1408.JPG"];
unsigned char *data = [self convertUIImageToData:image];
data = [self grayImageWithData:data originalImage:image];
self.imageViewGray.image = [self convertDataToUIImage:data image:image];
}
#pragma mark - 将UIImage转化为像素数据
- (unsigned char *)convertUIImageToData:(UIImage *)image{
CGSize size = image.size;
void *data = malloc(size.width * size.height * 4);
CGImageRef imageRef = [image CGImage];
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();//RGBA
CGContextRef contextRef = CGBitmapContextCreate(data, size.width, size.height, 8, 4 * size.width, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(contextRef, CGRectMake(0, 0, size.width, size.height), imageRef);
//mrc
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(contextRef);
return (unsigned char *)data;
}
#pragma mark - 对像素进行灰度处理
- (unsigned char *)grayImageWithData:(unsigned char *)imageData originalImage:(UIImage *)originalImage{
CGFloat width = originalImage.size.width;
CGFloat height = originalImage.size.height;
unsigned char *resultData = malloc(width * height * sizeof(unsigned char) * 4);
memset(resultData, 0, 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;
}
#pragma mark - 对像素进行颜色反转处理
- (unsigned char *)reColorImageWithData:(unsigned char *)imageData originalImage:(UIImage *)originalImage{
CGFloat width = originalImage.size.width;
CGFloat height = originalImage.size.height;
unsigned char *resultData = malloc(width * height * sizeof(unsigned char) * 4);
memset(resultData, 0, 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 bitMapRedRe = 255 - bitMapRed;
unsigned char bitMapGreenRe = 255 - bitMapGreen;
unsigned char bitMapBlueRe = 255 - bitMapBlue;
memset(resultData + imageIndex*4, bitMapRedRe, 1);
memset(resultData + imageIndex*4 + 1, bitMapGreenRe, 1);
memset(resultData + imageIndex*4 + 2, bitMapBlueRe, 1);
}
}
return resultData;
}
#pragma mark - 将像素数据转化为UIImage
- (UIImage *)convertDataToUIImage:(unsigned char *)imageData image:(UIImage *)imageSource{
CGFloat width = imageSource.size.width;
CGFloat height = imageSource.size.height;
CGFloat dataLength = width * height * 4;
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, imageData, dataLength, NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4*width, colorSpaceRef, kCGBitmapByteOrderDefault, dataProviderRef, NULL, NO, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef];
//mrc
CGImageRelease(imageRef);
CGDataProviderRelease(dataProviderRef);
CGColorSpaceRelease(colorSpaceRef);
return image;
}
网友评论