转自:http://www.cnblogs.com/YouXianMing/p/3719056.html
使用CoreImage教程

CoreImage包含有很多实用的滤镜,专业处理图片的库,为了能看到各种渲染效果,请使用如下图片素材.

现在可以开始教程了:

代码片段

代码片段

效果如下:

我们对操作进行简易的封装:
CIFilterEffect.h + CIFilterEffect.m
// // CIFilterEffect.h // CIFilter // // Created by YouXianMing on 14-5-9. // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> @interface CIFilterEffect : NSObject @property (nonatomic, strong, readonly) UIImage *result; - (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name; @end
// // CIFilterEffect.m // CIFilter // // Created by YouXianMing on 14-5-9. // Copyright (c) 2014年 Y.X. All rights reserved. // #import "CIFilterEffect.h" @interface CIFilterEffect () @property (nonatomic, strong, readwrite) UIImage *result; @end @implementation CIFilterEffect - (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name { self = [super init]; if (self) { // 将UIImage转换成CIImage CIImage *ciImage = [[CIImage alloc] initWithImage:image]; // 创建滤镜 CIFilter *filter = [CIFilter filterWithName:name keysAndValues:kCIInputImageKey, ciImage, nil]; [filter setDefaults]; // 获取绘制上下文 CIContext *context = [CIContext contextWithOptions:nil]; // 渲染并输出CIImage CIImage *outputImage = [filter outputImage]; // 创建CGImage句柄 CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]]; _result = [UIImage imageWithCGImage:cgImage]; // 释放CGImage句柄 CGImageRelease(cgImage); } return self; } @end
我们来开始尝试其他的滤镜效果,我们可以尝试的至少有这些:
@"CILinearToSRGBToneCurve",
@"CIPhotoEffectChrome",
@"CIPhotoEffectFade",
@"CIPhotoEffectInstant",
@"CIPhotoEffectMono",
@"CIPhotoEffectNoir",
@"CIPhotoEffectProcess",
@"CIPhotoEffectTonal",
@"CIPhotoEffectTransfer",
@"CISRGBToneCurveToLinear",
@"CIVignetteEffect",

下面是所有渲染出来的图片,与上面提供的滤镜名字一一对应:








以下效果是需要进行一些配置才能达到的效果,这个就不开源了,你懂得:).





福利:
Core Image Filter Reference
CICategoryBlur
CICategoryColorAdjustment
CIColorClampCIColorControlsCIColorMatrixCIColorPolynomialCIExposureAdjustCIGammaAdjustCIHueAdjustCILinearToSRGBToneCurveCISRGBToneCurveToLinearCITemperatureAndTintCIToneCurveCIVibranceCIWhitePointAdjust
CICategoryColorEffect(我们刚刚用到的一些效果在这里哦)
CIColorCrossPolynomialCIColorCubeCIColorCubeWithColorSpaceCIColorInvertCIColorMapCIColorMonochromeCIColorPosterizeCIFalseColorCIMaskToAlphaCIMaximumComponentCIMinimumComponentCIPhotoEffectChromeCIPhotoEffectFadeCIPhotoEffectInstantCIPhotoEffectMonoCIPhotoEffectNoirCIPhotoEffectProcessCIPhotoEffectTonalCIPhotoEffectTransferCISepiaToneCIVignetteCIVignetteEffect
CICategoryCompositeOperation
CIAdditionCompositingCIColorBlendModeCIColorBurnBlendModeCIColorDodgeBlendModeCIDarkenBlendModeCIDifferenceBlendModeCIExclusionBlendModeCIHardLightBlendModeCIHueBlendModeCILightenBlendModeCILuminosityBlendModeCIMaximumCompositingCIMinimumCompositingCIMultiplyBlendModeCIMultiplyCompositingCIOverlayBlendModeCISaturationBlendModeCIScreenBlendModeCISoftLightBlendModeCISourceAtopCompositingCISourceInCompositingCISourceOutCompositingCISourceOverCompositing
CICategoryDistortionEffect
CIBumpDistortionCIBumpDistortionLinearCICircleSplashDistortionCICircularWrapCIDrosteCIDisplacementDistortionCIGlassDistortionCIGlassLozengeCIHoleDistortionCILightTunnelCIPinchDistortionCIStretchCropCITorusLensDistortionCITwirlDistortionCIVortexDistortion
CICategoryGenerator
CICheckerboardGeneratorCIConstantColorGeneratorCILenticularHaloGeneratorCIQRCodeGeneratorCIRandomGeneratorCIStarShineGeneratorCIStripesGeneratorCISunbeamsGenerator
CICategoryGeometryAdjustment
CIAffineTransformCICropCILanczosScaleTransformCIPerspectiveTransformCIPerspectiveTransformWithExtentCIStraightenFilter
CICategoryGradient
CICategoryHalftoneEffect
CICategoryReduction
CIAreaAverageCIAreaHistogramCIRowAverageCIColumnAverageCIHistogramDisplayFilterCIAreaMaximumCIAreaMinimumCIAreaMaximumAlphaCIAreaMinimumAlpha
CICategorySharpen
CICategoryStylize
CIBlendWithAlphaMaskCIBlendWithMaskCIBloomCIComicEffectCIConvolution3X3CIConvolution5X5CIConvolution7X7CIConvolution9HorizontalCIConvolution9VerticalCICrystallizeCIDepthOfFieldCIEdgesCIEdgeWorkCIGloomCIHeightFieldFromMaskCIHexagonalPixellateCIHighlightShadowAdjustCILineOverlayCIPixellateCIPointillizeCIShadedMaterialCISpotColorCISpotLight
CICategoryTileEffect
CIAffineClampCIAffineTileCIEightfoldReflectedTileCIFourfoldReflectedTileCIFourfoldRotatedTileCIFourfoldTranslatedTileCIGlideReflectedTileCIKaleidoscopeCIOpTileCIParallelogramTileCIPerspectiveTileCISixfoldReflectedTileCISixfoldRotatedTileCITriangleKaleidoscopeCITriangleTileCITwelvefoldReflectedTile
CICategoryTransition
CIBarsSwipeTransitionCICopyMachineTransitionCIDisintegrateWithMaskTransitionCIDissolveTransitionCIFlashTransitionCIModTransitionCIPageCurlTransitionCIPageCurlWithShadowTransitionCIRippleTransitionCISwipeTransition
标签:
iOS

本文介绍如何使用CoreImage库实现多种图片滤镜效果,包括线性和sRGB色调曲线转换、不同风格的照片效果及晕影效果等。通过简单的封装,开发者能够轻松地将这些效果应用于iOS应用中。

1664

被折叠的 条评论
为什么被折叠?



