#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 滤镜效果
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 其中选图按钮的触发事件方法名为:SelectImage保存按钮的触发事件方法名为:SaveCore Image利用...
- 介绍 1、框架介绍 (1)CoreImage,不用导入(2)是一个图像框架 它基于OpenGL顶层创建 底层则用着...