天天看點

IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能

如今對個人圖檔的美化可所謂,天花亂醉。今天,就和大家一起進入一個美麗的世界。

第一步:首先,了解一個重要的知識,如下:

   UIImage和UIView使用的是左上原點坐标,Core Image和Core Graphics使用的是左下原點坐标

第二部:效果圖預覽。

IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能
IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能
IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能
IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能
IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能
IOS美圖秀秀(濾鏡和塗鴉)和 添加陰影功能

第二部:我們就要上代碼了。代碼一共 266行。不好之處,請多包含。

#import "ViewController.h"

@interface ViewController ()

{

    @protected

    //建立圖像上下文

    CIContext * Context ;

    // 建立顯示圖檔的控件

    UIImageView * ImageV;

    //我們要處理的圖像

    CIImage * WillDoImage;

    //處理後,我們得到的圖像

    CIImage * OutImage;

    //添加濾鏡鏡頭

    CIFilter * Filter_zsj;

    @public

    // 濾鏡功能背景圖

    UIView * View ;

}

@property(nonatomic,strong) NSMutableArray * AllPointArray;

// 标記是否可以塗鴉

@property(nonatomic,assign) BOOL TuYaBool;

//系統暫時圖像

@property(nonatomic,strong) UIImage * Temp_Image;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.AllPointArray = [NSMutableArray arrayWithCapacity:0];

    self.Temp_Image = [UIImage imageNamed:@"4673e28380ac5b03ab2f8da9bb78d9e4.jpg"];

    [self loadNav];

    [self makeUI];

    // 建立濾鏡的功能按鈕

    [self filterBtn];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)loadNav{

    UILabel * Nav_Label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-100, 20, 200, 44)];

    Nav_Label.textAlignment = NSTextAlignmentCenter;

    Nav_Label.text = @"成功QQ吧提供--濾鏡";

    Nav_Label.textColor = [UIColor magentaColor];

    Nav_Label.font = [UIFont fontWithName:@"" size:22];

    Nav_Label.shadowColor = [UIColor lightGrayColor];

    Nav_Label.shadowOffset = CGSizeMake(5, -5);

   [self.view addSubview:Nav_Label];

    UIView * Line =[[UIView alloc] initWithFrame:CGRectMake(0, 63.5, self.view.bounds.size.width, 0.5) ];

    Line.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview: Line];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//圖形布局

-(void)makeUI{

    // 添加加載控件(image)

    ImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height-65)];

    // 設定圖檔的顯示模式

    ImageV.contentMode = UIViewContentModeScaleToFill;

    //顯示圖像

    [self.view addSubview:ImageV];

    // 建立圖像上下文

    [self getcontext];

    // 建立濾鏡,鏡頭

    [self createFilter];

    //開始繪圖

    [self createImage];

    // 進行圖檔的顯示

    [self showImage];

}

-(void)getcontext{

    // 直接擷取,以免處理優先級降低

    Context = [CIContext contextWithOptions:nil];

    // 第二種:

    // 使用OpenGl 我們選着 最低的版本

    // 還有第三種

}

-(void)createFilter{

    Filter_zsj = [CIFilter filterWithName:@"CIColorControls"] ;

}

-(void)createImage{

    ImageV.image = self.Temp_Image;

    //擷取圖像

    WillDoImage = [CIImage imageWithCGImage:ImageV.image.CGImage];

    //讓濾鏡鏡頭捕捉到她

    [Filter_zsj setValue:WillDoImage forKey:@"inputImage"];

}

-(void)showImage{

    // 擷取濾鏡後的圖檔

    CIImage * OverImage = [Filter_zsj outputImage];

    // 建立圖像上下文

    // extent  Extent(xmin, ymin, xmax, ymax, spatialReference) 建立一個範圍對象。坐标表示邊界框左下角和右上角的坐标

    CGImageRef   ImageRef = [Context createCGImage:OverImage fromRect: [OverImage extent]];

    // 從圖像上下文擷取圖像

    ImageV.image = [UIImage imageWithCGImage:ImageRef];

    CGImageRelease(ImageRef);

}

-(void)filterBtn{

    UIView * Filter_View = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-50, self.view.frame.size.width, 50)];

    Filter_View.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:Filter_View];

    NSArray * Btn_titleArray = @[@"濾鏡",@"磨皮",@"瘦身",@"塗鴉"];

    for (int i =0 ;i< Btn_titleArray.count ;i++){

        UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];

        Button.frame = CGRectMake(10 + i* (80+(self.view.frame.size.width-340)/3), 5, 80, 40);

        Button.layer.cornerRadius = 6;

        [Button.layer setBorderWidth: 0.5];

        Button.tag = i;

        [Button setTitle:Btn_titleArray[i] forState:UIControlStateNormal];

        [Button addTarget:self action:@selector(filterClick:) forControlEvents:UIControlEventTouchUpInside];

        [Filter_View addSubview:Button];

    }

}

//圖像濾鏡處理

-(void)filterClick:(UIButton*)filterbtn{

    switch (filterbtn.tag) {

        case 0:

        {

            if (filterbtn.selected) {

                filterbtn.selected = NO;

                [self deallocView];

            }else{

            filterbtn.selected = YES;

            [self filterstar];

            }

        }

            break;

        case 1:{

        }

            break;

        case 2:

            break;

        case 3:

            if (filterbtn.selected) {

                filterbtn.selected = NO;

                self.TuYaBool = NO;

            }else{

                filterbtn.selected = YES;

                self.TuYaBool = YES;

            }

            break;

        default:

            break;

    }

}

-(void)filterstar{

    View = [[UIView alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height-210, self.view.frame.size.width-20, 150)];

    View.backgroundColor = [UIColor whiteColor];

    View.layer.cornerRadius = 6;

    [self.view addSubview:View];

    NSArray * ColoerFunction = @[@"亮    度 :",@"飽和度 :",@"對比度 :"];

    for (int i =0 ; i< ColoerFunction.count; i++) {

        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10,10+ i * 50, 70, 40)];

        label.text = ColoerFunction[i];

        label.textColor = [UIColor redColor];

        [View addSubview:label];

        UISlider * Slider = [[UISlider alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame), 10+ i * 50, CGRectGetWidth(View.frame)-CGRectGetWidth(label.frame)-25, 40)];

        Slider.maximumValue = 1.0;

        Slider.minimumValue = 0.0;

        Slider.tag = 100+i;

        Slider.thumbTintColor = [UIColor greenColor];

        Slider.maximumTrackTintColor = [UIColor purpleColor];

        [Slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];

        [View addSubview:Slider];

    }

}

// 背景圖的消失

-(void)deallocView{

    [View removeFromSuperview];

}

// 功能

-(void)slider:(UISlider*)slider{

    switch (slider.tag - 100) {

        case 0:{

            // 亮度

            [Filter_zsj setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputBrightness"];

            [self showImage];

        }

            break;

        case 1:{

            // 飽和度 注意:飽和度的數值

            [Filter_zsj setValue:[NSNumber numberWithFloat:(1.0-slider.value)] forKey:@"inputSaturation"];

            [self showImage];

        }

            break;

        case 2:{

            // 對比度

            [Filter_zsj setValue:[NSNumber numberWithFloat:(1.0-slider.value)] forKey:@"inputContrast"];

            [self showImage];

        }

            break;

        default:

            break;

    }

}

// 塗鴉

-(void)tuya{

    // 擷取圖像的上下文

    for (int i = 0 ; i<_AllPointArray.count; i++) {

        for (int j =0 ; j<(int)([_AllPointArray[i] count]-1); j++) {

            UIGraphicsBeginImageContext(ImageV.frame.size);

            [ImageV.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-65)];

            // 取得第一個

            CGPoint onepoint = [_AllPointArray[i][j] CGPointValue];

            // 擷取下一個

            CGPoint nextpoint = [_AllPointArray[i][j+1] CGPointValue];

            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), onepoint.x, onepoint.y);

            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), nextpoint.x, nextpoint.y);

            [[UIColor redColor] set];

            CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke);

            ImageV.image = UIGraphicsGetImageFromCurrentImageContext();

        }

    }

    self.Temp_Image = ImageV.image;

    [self createImage];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    if (self.TuYaBool) {

        NSMutableArray * pointArray = [NSMutableArray arrayWithCapacity:0];

        [_AllPointArray addObject:pointArray];

    }

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    if (self.TuYaBool) {

        UITouch * touch = [touches anyObject];

        CGPoint  point = [touch locationInView:ImageV];

        NSValue * value = [NSValue valueWithCGPoint:point];

        [[_AllPointArray lastObject] addObject:value];

        [self tuya];

    }

}

@end