1. 照片变黑白(遍历像素)
CGImageRef imageRef = self.imageView.image.CGImage; // 一个字节 = 8bit(位) 每行有 17152 个字节,即每行有 17152 * 8 个位 一个像素有 4 个字节 size_t width = CGImageGetWidth(imageRef); size_t height = CGImageGetHeight(imageRef); size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); // 颜色空间 uint32_t bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef space = CGImageGetColorSpace(imageRef); //获取每个像素的字节数据集合 CGDataProviderRef dataProviderRef = CGImageGetDataProvider(imageRef); CFDataRef dataRef = CGDataProviderCopyData(dataProviderRef); UInt8 * bytesPtr = CFDataGetMutableBytePtr((CFMutableDataRef)dataRef); NSUInteger length = CFDataGetLength(dataRef); // 一个像素由四个字节byte组成 for (int i = 0; i < length; i += 4) { UInt8 red = bytesPtr[i]; UInt8 green = bytesPtr[i + 1]; UInt8 blue = bytesPtr[i + 2]; UInt8 average = (red + green + blue) / 3; bytesPtr[i] = average; bytesPtr[i + 1] = average; bytesPtr[i + 2] = average; } CGContextRef imageContextRef = CGBitmapContextCreate(bytesPtr, width, height, bitsPerComponent, bytesPerRow, space, bitmapInfo); CGImageRef finalImageRef = CGBitmapContextCreateImage(imageContextRef); UIImage * finalImage = [UIImage imageWithCGImage:finalImageRef]; self.imageView.image = finalImage; CGImageRelease(finalImageRef); CGContextRelease(imageContextRef); CFRelease(dataRef);
2. 照片饱和度调整
CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage]; CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"]; [filter setValue:beginImage forKey:kCIInputImageKey]; //饱和度 0---2 默认为1 [filter setValue:[NSNumber numberWithFloat:self.slider.value*2] forKey:@"inputSaturation"]; // 得到过滤后的图片 CIImage *outputImage = [filter outputImage]; // 转换图片, 创建基于GPU的CIContext对象 CIContext *context = [CIContext contextWithOptions: nil]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *newImg = [UIImage imageWithCGImage:cgimg]; // 显示图片 [self.imageView setImage:newImg]; // 释放C对象 CGImageRelease(cgimg);
3. 照片亮度调整
CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage]; CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"]; [filter setValue:beginImage forKey:kCIInputImageKey]; //亮度 -1---1 默认为0 [filter setValue:[NSNumber numberWithFloat:(self.slider.value-0.5)*2] forKey:@"inputBrightness"]; // 得到过滤后的图片 CIImage *outputImage = [filter outputImage]; // 转换图片, 创建基于GPU的CIContext对象 CIContext *context = [CIContext contextWithOptions: nil]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *newImg = [UIImage imageWithCGImage:cgimg]; // 显示图片 [self.imageView setImage:newImg]; // 释放C对象 CGImageRelease(cgimg);
4. 照片对比度调整
CIImage *beginImage = [CIImage imageWithCGImage:self.imageView.image.CGImage]; CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"]; [filter setValue:beginImage forKey:kCIInputImageKey]; //对比度 0---4 默认为1 [filter setValue:[NSNumber numberWithFloat:self.slider.value*4.00f] forKey:@"inputContrast"]; // 得到过滤后的图片 CIImage *outputImage = [filter outputImage]; // 转换图片, 创建基于GPU的CIContext对象 CIContext *context = [CIContext contextWithOptions: nil]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *newImg = [UIImage imageWithCGImage:cgimg]; // 显示图片 [self.imageView setImage:newImg]; // 释放C对象 CGImageRelease(cgimg);
5. 照片添加纯色滤镜
UIGraphicsBeginImageContext(CGSizeMake(self.imageView.image.size.width, self.imageView.image.size.height)); CGContextRef imageContextRef = UIGraphicsGetCurrentContext(); [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height)]; //红色滤镜 UIColor *color = [UIColor clearColor]; switch (self.segmentedControl.selectedSegmentIndex) { case 0://红 color = [UIColor colorWithRed:1 green:0 blue:0 alpha:self.slider.value]; break; case 1://绿 color = [UIColor colorWithRed:0 green:1 blue:0 alpha:self.slider.value]; break; case 2://蓝 color = [UIColor colorWithRed:0 green:0 blue:1 alpha:self.slider.value]; break; default: break; } // 给上下文设置颜色 CGContextSetFillColorWithColor(imageContextRef, color.CGColor); // 设置渲染模式 CGContextSetBlendMode(imageContextRef, kCGBlendModeNormal); // 填充上下文 CGContextFillRect(imageContextRef, CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height)); CGImageRef finalImageRef = CGBitmapContextCreateImage(imageContextRef); UIImage * finalImage = [UIImage imageWithCGImage:finalImageRef]; self.imageView.image = finalImage; CGImageRelease(finalImageRef); CGContextRelease(imageContextRef);
参考自:https://www.jianshu.com/p/d375432ebb0d