美文网首页
iOS 滤镜效果

iOS 滤镜效果

作者: emily_sky | 来源:发表于2016-08-18 14:21 被阅读84次
    Simulator Screen Shot 2016年8月18日 下午2.00.24.png Simulator Screen Shot 2016年8月18日 下午2.01.28.png
    #import "ViewController.h"
    @interface ViewController (){
        UIImageView *imageView;
        NSArray *array;
        NSOperationQueue *queue;// 多线程(操作队列)中去执行,而且是异步执行的。)
    }  
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    /*
     需求1:屏幕上边显示着一张图片,在下方有一个scalorView可以滑动
     里面填充的是已经渲染好的图片
     2.通过菊花器来加载图片,在子线程中加载
     3.图片的渲染是带有动画效果
     4.子线程改变图片的coreImage不要卡UI
     ps:使用线程的时候不要使用全局变量,在for循环里面,否则只有最后一个能附上值
     
     */
        int viewWidth=[[UIScreen mainScreen]bounds].size.width;
        int viewHeight=[[UIScreen mainScreen]bounds].size.height;
    
        queue=[[NSOperationQueue alloc]init];//创建操作队列
    
        array=@[@"CIPhotoEffectMono",@"CIPhotoEffectTonal",@"CIPhotoEffectNoir",@"CIPhotoEffectFade",@"CIPhotoEffectChrome",@"CIPhotoEffectProcess",@"CIPhotoEffectTransfer",@"CIPhotoEffectInstant",@"CISRGBToneCurveToLinear"];
    
        CGRect imageViewRect=CGRectMake(0, 0, viewWidth, viewHeight-150);
    
        imageView=[[UIImageView alloc]initWithFrame:imageViewRect];
        imageView.image=[UIImage imageNamed:@"4.jpg"];
        imageView.contentMode=UIViewContentModeScaleAspectFit;//图片自适应
        [self.view addSubview:imageView];
    
        CGRect scrollViewRect=CGRectMake(0, viewHeight-150, viewWidth, 150);
        UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:scrollViewRect];
        scrollView.backgroundColor=[UIColor orangeColor];
        scrollView.contentSize=CGSizeMake(110*9-10, 130);
    
        for (int i=0; i<9; i++) {
            //photo
            CGRect imageViewRect=CGRectMake((100 +10)*i, 10, 100, 130);
            UIImageView *imageViewScroll=[[UIImageView alloc]initWithFrame:imageViewRect];
            imageViewScroll.contentMode=UIViewContentModeScaleAspectFit;
            [scrollView addSubview:imageViewScroll];
            //活动指示器
            UIActivityIndicatorView *indicatorView=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            
            indicatorView.center=CGPointMake(imageViewScroll.frame.size.width/2, imageViewScroll.frame.size.height/2);
    
            [indicatorView startAnimating];
    
            [imageViewScroll addSubview:indicatorView];
    
            //tag各单击事件
            UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            imageViewScroll.tag=i;
            [imageViewScroll addGestureRecognizer:tap];
            imageViewScroll.userInteractionEnabled=YES;
            //多线程
            NSBlockOperation *photo_block=[NSBlockOperation blockOperationWithBlock:^{
            //加载图片
            UIImage *image=[self filterImage:array[i] originImage:[UIImage imageNamed:@"4.jpg"]];
            [imageViewScroll performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
            //停止菊花器
            NSBlockOperation *stopIndicator=[NSBlockOperation blockOperationWithBlock:^{
                [self performSelectorOnMainThread:@selector(updateIndicator:) withObject:indicatorView waitUntilDone:YES];
                }];
                [queue addOperation:stopIndicator];
            }];
            [queue addOperation:photo_block];
        }
        [self.view addSubview:scrollView];
    }
    -(void)tapAction:(UITapGestureRecognizer *)sender{
        UIImageView *imageV=(UIImageView *)sender.view;
        imageView.image=imageV.image;
    }
    //filter
    -(UIImage *)filterImage:(NSString *)type originImage:(UIImage *)image{
        CIContext *context=[CIContext contextWithOptions:nil];
        CIImage *resultImage=[[CIImage alloc]initWithCGImage:image.CGImage];
        //系统滤镜
        CIFilter *filter=[CIFilter filterWithName:type];
        //传入要过滤的图片
        [filter setValue:resultImage forKey:kCIInputImageKey];
        //输出过滤后图片
        resultImage =filter.outputImage;
        CGRect extent=[resultImage extent];
        CGImageRef cgImage=[contextcreateCGImage:resultImage fromRect:extent];
        UIImage *result=[UIImage imageWithCGImage:cgImage];
        return result;
    }
    //stop 活动指示器
    -(void)updateIndicator:(UIActivityIndicatorView *)sender{
        [sender stopAnimating];
        [sender removeFromSuperview];
    }
    @end

    相关文章

      网友评论

          本文标题:iOS 滤镜效果

          本文链接:https://www.haomeiwen.com/subject/kudvsttx.html