//使用NSUserDefaults对UIImage数据进行保存 1.对JPEG图片进行保存 UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality); 2.对PNG图片进行保存 UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image); //保存(这里保存图片主要是为了后续直接拿来用,使用GPUImage对图片进行模糊时,是一件即费时间,又费CPU的事情) [[NSUserDefaults standardUserDefaults] setValue:imageData forKey:@"image"]; [[NSUserDefaults standardUserDefaults] synchronize]; #import "ViewController.h" #import <GPUImage/GPUImage.h> @interface ViewController () { GPUImageiOSBlurFilter * _blurFilter; UIImageView * imageView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; imageView.image = [UIImage imageNamed:@"IMG_0311.JPG"]; [self.view addSubview:imageView]; _blurFilter = [[GPUImageiOSBlurFilter alloc]init]; _blurFilter.blurRadiusInPixels = 4.0; [self performScreenshotAndBlur]; } -(void) performScreenshotAndBlur { UIImage * image = [self convertViewToImage]; UIImage *blurredSnapshotImage = [_blurFilter imageByFilteringImage:image]; [imageView setImage:blurredSnapshotImage]; imageView.alpha = 1.0; [self.view addSubview:imageView]; } -(UIImage *)convertViewToImage { UIGraphicsBeginImageContext(self.view.bounds.size); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小为 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片 UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文 NSLog(@"%@",NSStringFromCGSize(viewImage.size)); //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图 return viewImage; }
时间: 2024-11-08 18:59:15