//通过修改图片饱和度将图片变为黑白(镂空位置不影响)
- (UIImage*) convertImageToGreyScale:(UIImage*) image
{
CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];
CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
[filter setValue:beginImage forKey:kCIInputImageKey];
//饱和度 0---2 默认为1
[filter setValue:[NSNumber numberWithFloat:0] 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];
// 释放C对象
CGImageRelease(cgimg);
return newImg;
}
//将图片变为黑边反转(镂空部位为黑色)
- (UIImage*) convertImageToGreyScale:(UIImage*) image {
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGContextDrawImage(context, imageRect, [image CGImage]);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpace); CGContextRelease(context);
CFRelease(imageRef);
return newImage;
}