1. 程式人生 > >iOS中濾鏡的使用(二)濾鏡組合

iOS中濾鏡的使用(二)濾鏡組合

iOS中濾鏡的使用(二)

濾鏡組合

首先 要載入圖片並轉化為CIImage

CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0160"]];

其次,建立filter濾鏡

 CIFilter *filterOne = [CIFilter filterWithName:@"CIPixellate"];//馬賽克濾鏡

    NSLog(@"%@",filterOne.attributes);
    [filterOne setValue:ciImage  forKey:kCIInputImageKey];
    [filterOne setDefaults];
    CIImage *outImage = [filterOne valueForKey:kCIOutputImageKey];

    CIFilter *filterTwo = [CIFilter filterWithName:@"CIHueAdjust"];//飽和度濾鏡
[filterTwo setValue:outImage forKey:kCIInputImageKey]; [filterTwo setDefaults]; NSLog(@"%@",filterTwo.attributes);//列印可修改的屬性 [filterTwo setValue:@(3.14) forKey:kCIInputAngleKey];//位置不能改變 CIImage *outImageTwo = [filterTwo valueForKey:kCIOutputImageKey];

然後,用CIContext將濾鏡中的圖片渲染出來,原因,匯出圖片過過程中用到CGImageRef。

CIContext *context = [CIContext contextWithOptions:nil];

CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]];

最後,匯出圖片,注意記憶體的釋放

 UIImage *showImage = [UIImage imageWithCGImage:cgImage];

CGImageRelease(cgImage);

剩下的工作就是載入馬賽克圖片了

 UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0
, 0, 600, 400)]; imageView.image = showImage; imageView.center = self.view.center; [self.view addSubview:imageView];

而馬賽克Filter的屬性有如下:
馬賽克屬性

飽和度Filter的屬性如下:
飽和度屬性

總的程式碼如下:

 //0:匯入圖片

    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0160"]];
    //1:建立filter組合濾鏡
      CIFilter *filterOne = [CIFilter filterWithName:@"CIPixellate"];//馬賽克濾鏡

    NSLog(@"%@",filterOne.attributes);
    [filterOne setValue:ciImage  forKey:kCIInputImageKey];
    [filterOne setDefaults];
    CIImage *outImage = [filterOne valueForKey:kCIOutputImageKey];

    CIFilter *filterTwo = [CIFilter filterWithName:@"CIHueAdjust"];//飽和度濾鏡

    [filterTwo setValue:outImage  forKey:kCIInputImageKey];
    [filterTwo setDefaults];

    NSLog(@"%@",filterTwo.attributes);//列印可修改的屬性
    [filterTwo setValue:@(3.14) forKey:kCIInputAngleKey];//位置不能改變

    CIImage *outImageTwo = [filterTwo valueForKey:kCIOutputImageKey];


    //2:用CIContext將濾鏡中的圖片渲染出來

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]];

    //3:匯出圖片

    UIImage *showImage = [UIImage imageWithCGImage:cgImage];

    CGImageRelease(cgImage);

    //4:加載出來

    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 600, 400)];
    imageView.image = showImage;
    imageView.center = self.view.center;
    [self.view addSubview:imageView];

濾鏡組合的種類有如下:
//Iphone中目前可用的
CIAdditionCompositing //影像合成

CIAffineTransform           //仿射變換

CICheckerboardGenerator       //棋盤發生器

CIColorBlendMode              //CIColor混合模式

CIColorBurnBlendMode          //CIColor燃燒混合模式

CIColorControls 

CIColorCube                   //立方體

CIColorDodgeBlendMode         //CIColor避免混合模式

CIColorInvert                 //CIColor反相

CIColorMatrix                 //CIColor矩陣

CIColorMonochrome             //黑白照

CIConstantColorGenerator      //恆定顏色發生器

CICrop                        //裁剪

CIDarkenBlendMode             //亮度混合模式

CIDifferenceBlendMode         //差分混合模式

CIExclusionBlendMode          //互斥混合模式

CIExposureAdjust              //曝光調節

CIFalseColor                  //偽造顏色

CIGammaAdjust                 //灰度係數調節

CIGaussianGradient            //高斯梯度

CIHardLightBlendMode          //強光混合模式

CIHighlightShadowAdjust       //高亮陰影調節

CIHueAdjust                   //飽和度調節

CIHueBlendMode                //飽和度混合模式

CILightenBlendMode            

CILinearGradient              //線性梯度

CILuminosityBlendMode         //亮度混合模式

CIMaximumCompositing          //最大合成

CIMinimumCompositing          //最小合成

CIMultiplyBlendMode           //多層混合模式

CIMultiplyCompositing         //多層合成

CIOverlayBlendMode            //覆蓋疊加混合模式

CIRadialGradient              //半徑梯度

CISaturationBlendMode         //飽和度混合模式

CIScreenBlendMode             //全屏混合模式

CISepiaTone                   //棕黑色調

CISoftLightBlendMode          //弱光混合模式

CISourceAtopCompositing       

CISourceInCompositing 

CISourceOutCompositing 

CISourceOverCompositing 

CIStraightenFilter            //拉直過濾器

CIStripesGenerator            //條紋發生器

CITemperatureAndTint          //色溫

CIToneCurve                   //色調曲線

CIVibrance                    //振動

CIVignette                    //印花

CIWhitePointAdjust            //白平衡調節

官方文件中列表:

CICategoryBlur
CIBoxBlur
CIDiscBlur
CIGaussianBlur
CIMedianFilter
CIMotionBlur
CINoiseReduction
CIZoomBlur

CICategoryColorAdjustment
CIColorControls
CIColorMatrix
CIExposureAdjust
CIGammaAdjust
CIHueAdjust
CITemperatureAndTint
CIToneCurve
CIVibrance
CIWhitePointAdjust

CICategoryColorEffect
CIColorCube
CIColorInvert
CIColorMap
CIColorMonochrome
CIColorPosterize
CIFalseColor
CIMaskToAlpha
CIMaximumComponent
CIMinimumComponent
CISepiaTone

CICategoryCompositeOperation
CIAdditionCompositing
CIColorBlendMode
CIColorBurnBlendMode
CIColorDodgeBlendMode
CIDarkenBlendMode
CIDifferenceBlendMode
CIExclusionBlendMode
CIHardLightBlendMode
CIHueBlendMode
CILightenBlendMode
CILuminosityBlendMode
CIMaximumCompositing
CIMinimumCompositing
CIMultiplyBlendMode
CIMultiplyCompositing
CIOverlayBlendMode
CISaturationBlendMode
CIScreenBlendMode
CISoftLightBlendMode
CISourceAtopCompositing
CISourceInCompositing
CISourceOutCompositing
CISourceOverCompositing

CICategoryDistortionEffect
CIBumpDistortion
CIBumpDistortionLinear
CICircleSplashDistortion
CICircularWrap
CIDisplacementDistortion
CIGlassDistortion
CIGlassLozenge
CIHoleDistortion
CIPinchDistortion
CITorusLensDistortion
CITwirlDistortion
CIVortexDistortion

CICategoryGenerator
CICheckerboardGenerator
CIConstantColorGenerator
CILenticularHaloGenerator
CIRandomGenerator
CIStarShineGenerator
CIStripesGenerator
CISunbeamsGenerator

CICategoryGeometryAdjustment
CIAffineTransform
CICrop
CILanczosScaleTransform
CIPerspectiveTransform
CIStraightenFilter

CICategoryGradient
CIGaussianGradient
CILinearGradient
CIRadialGradient

CICategoryHalftoneEffect
CICircularScreen
CICMYKHalftone
CIDotScreen
CIHatchedScreen
CILineScreen

CICategoryReduction
CIAreaAverage
CIAreaHistogram
CIRowAverage
CIColumnAverage
CIAreaMaximum
CIAreaMinimum
CIAreaMaximumAlpha
CIAreaMinimumAlpha

CICategorySharpen
CISharpenLuminance
CIUnsharpMask

CICategoryStylize
CIBlendWithMask
CIBloom
CIComicEffect
CICrystallize
CIEdges
CIEdgeWork
CIGloom
CIHeightFieldFromMask
CIHexagonalPixellate
CIHighlightShadowAdjust
CILineOverlay
CIPixellate
CIPointillize
CIShadedMaterial
CISpotColor
CISpotLight

CICategoryTileEffect
CIAffineClamp
CIAffineTile
CIEightfoldReflectedTile
CIFourfoldReflectedTile
CIFourfoldRotatedTile
CIFourfoldTranslatedTile
CIGlideReflectedTile
CIKaleidoscope
CIOpTile
CIParallelogramTile
CIPerspectiveTile
CISixfoldReflectedTile
CISixfoldRotatedTile
CITriangleTile
CITwelvefoldReflectedTile

CICategoryTransition
CIBarsSwipeTransition
CICopyMachineTransition
CIDisintegrateWithMaskTransition
CIDissolveTransition
CIFlashTransition
CIModTransition
CIPageCurlTransition
CIRippleTransition
CISwipeTransition