HTCoreImage 是 Core Image 方便类别的集合。每个滤镜都有一个方便的构造器,并使用 NS_AVAILABLE_SINCE() 宏进行了注释,以便您知道 iOS 5 和 iOS 6 中的内容。
UIImage *sourceUIImage = [UIImage imageNamed:@"asdf"];
[[[sourceUIImage toCIImage] imageByApplyingFilters:@[
[CIFilter filterColorControlsSaturation:1 brightness:1 contrast:2],
[CIFilter filterColorMatrixWithRed:0.5 green:0.5 blue:1 alpha:1]]]
processToUIImageCompletion:^(UIImage *uiImage) {
NSLog(@"%@", uiImage);
}];
UIImage *sourceUIImage = [UIImage imageNamed:@"asdf"];
CIImage *sourceCIImage = [CIImage imageWithCGImage:sourceUIImage.CGImage];
CIFilter *colorControlsFilter = [CIFilter filterWithName:@"CIColorControls"];
[colorControlsFilter setDefaults];
[colorControlsFilter setValue:@(1) forKey:@"inputSaturation"];
[colorControlsFilter setValue:@(1) forKey:@"inputBrightness"];
[colorControlsFilter setValue:@(2) forKey:@"inputContrast"];
[colorControlsFilter setInputCIImage:sourceCIImage];
CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"];
[colorMatrixFilter setDefaults];
[colorMatrixFilter setValue:[CIVector vectorWithX:0.5 Y:0 Z:0 W:0]
forKey:@"inputRVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0.5 Z:0 W:0]
forKey:@"inputGVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0]
forKey:@"inputBVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1]
forKey:@"inputAVector"];
[colorMatrixFilter setInputCIImage:[colorControlsFilter outputImage]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
CIContext *ciContext = [CIContext contextWithOptions:@{ kCIContextUseSoftwareRenderer : (id)kCFBooleanFalse }];
CGImageRef resultCGImage = [ciContext createCGImage:[colorMatrixFilter outputImage] fromRect:[[colorMatrixFilter outputImage] extent]];
UIImage *resultUIImage = [UIImage imageWithCGImage:resultCGImage scale:sourceUIImage.scale orientation:sourceUIImage.imageOrientation];
CGImageRelease(resultCGImage);
dispatch_async(dispatch_get_main_queue(), ^
{
NSLog(@"%@", resultUIImage);
});
});
推荐安装方法为 Cocoapods。将此行添加到您的 Podfile
pod 'HTCoreImage'
向作者 @jakejennings 发推特,并查看 HotelTonight 的工程博客:http://engineering.hoteltonight.com
此外,请查看 HotelTonight 的其他 iOS 开源项目