IOS picture background Blur effect

Source: Internet
Author: User

1. Using the blur filter in Coreimage

The original is as follows:

Implementation of Coreimage:

- (void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib.    UIImage* image = [UIImageimagenamed:@"icon"];/*.. Blur effect filter in Coreimage: */    //ciimage, equivalent to UIImage, functions for acquiring picture ResourcesCiimage * Ciimage = [[Ciimage alloc]initwithimage:image];//cifilter, Gaussian blur filterCifilter * Blurfilter = [Cifilter filterwithname:@"Cigaussianblur"];//Enter the picture into the filter[Blurfilter setvalue:ciimage Forkey:kciinputimagekey];//To query the parameters that the filter can set and some related information    NSLog(@"%@", [Blurfilter attributes]);//Set blur level, default is 10, value range (0-100)[Blurfilter setvalue:@ ( -) forkey:@"Inputradius"];//The picture output will be handled wellCiimage * Outciimage = [Blurfilter Valueforkey:kcioutputimagekey];//cicontextCicontext * context = [Cicontext contextwithoptions:Nil];//Get the cgimage handle, that is, remove the picture from the data streamCgimageref outcgimage = [Context createcgimage:outciimage fromrect:[outciimage extent];//Final capture to picture    UIImage* Blurimage = [UIImageImagewithcgimage:outcgimage];//release cgimage handleCgimagerelease (Outcgimage);Uiimageview* ImageView = [[UiimageviewAlloc]initwithframe: Self. View. Bounds]; ImageView. Image= Blurimage; [ Self. ViewAddsubview:imageview];}
[Blurfilter attributes] printing results are as follows://parameters and some related information, if you do not make any settings, the default degree isTen(Ciattributedefault =Ten); {"Ciattributefilteravailable_mac"="10.4";"Ciattributefilteravailable_ios"=6; Ciattributefiltercategories = (Cicategoryblur, cicategorystillimage, Cicategoryvideo, CIC    Ategorybuiltin); Ciattributefilterdisplayname ="Gaussian Blur";    Ciattributefiltername = Cigaussianblur; Ciattributereferencedocumentation ="Http://developer.apple.com/cgi-bin/apple_ref.cgi?apple_ref=//apple_ref/doc/filter/ci/CIGaussianBlur";        Inputimage = {Ciattributeclass = Ciimage; Ciattributedescription =The image to use as an input image. For filters so also use a background image, this is the foreground image. ";        Ciattributedisplayname = Image;    Ciattributetype = Ciattributetypeimage;    };        Inputradius = {Ciattributeclass = NSNumber; Ciattributedefault =Ten; Ciattributedescription ="The radius determines how many pixels is used to create the blur. The larger the radius, the blurrier the result. ";        Ciattributedisplayname = Radius; Ciattributeidentity =0; Ciattributeminch=0; Ciattributeslidermax = -; Ciattributesliderminch=0;    Ciattributetype = Ciattributetypescalar; };}

The default level is 10 as follows:

The default level is 50 as follows:

2. Using uiimage+imageeffects

To the address download: Https://github.com/YouXianMing/UIImageBlur, there are many good things, it is worth attention;
Import uiimage+imageeffects files into the project can be used;

Implementation of Uiimage+imageeffects:

UIImage* image = [UIImageimagenamed:@"icon"];//Blur effect rendering time consuming, recommended to render in child threads//blur the picture    UIImage* Blurimage = [image blurimage];//Set blur level    UIImage* BlurImage1 = [Image blurimagewithradius:Ten];//Set local blur degree    UIImage* BlurImage2 = [Image Blurimageatframe:cgrectmake (0,0, image. Size. Width, image. Size. Height/2)];//Grayscale effect    UIImage* BlurImage3 = [image grayscale];/* Other methods, you can try to use//fixed width and fixed height-(UIImage *) Scalewithfixedwidth: (cgfloat) width;     -(UIImage *) Scalewithfixedheight: (cgfloat) height;     Average Color-(Uicolor *) Averagecolor;     Crop a part of a picture-(UIImage *) Croppedimageatframe: (CGRect) frame; */    Uiimageview* ImageView = [[UiimageviewAlloc]initwithframe: Self. View. Bounds]; ImageView. Image= Blurimage; [ Self. ViewAddsubview:imageview];

The following are the differences:

Local Blur

Grey

3. Using Uivisualeffectview

Note: Uivisualeffectview only applies to users iOS 8 or above, and can only be used if the following 8 is not available;

Implementation of Uivisualeffectview:

1. The text content is added directly to the Effectview (the text content in this case cannot be in accordance with the background);

UIImage * image = [UIImage imagenamed:@"icon"];Uiimageview * ImageView = [[Uiimageview alloc]initwithframe:self. View. Bounds];ImageView. Image= Image;[Self. ViewAddsubview:imageview];Uiblureffect * blur = [Uiblureffect effectwithstyle:uiblureffectstylelight];Create Blur View Uivisualeffectview * Effectview = [[Uivisualeffectview Alloc]initwitheffect:blur];Effectview. Frame= CGRectMake (0, -, ImageView. Frame. Size. Width, $);[ImageView Addsubview:effectview];Add display text UILabel * lable = [[UILabel Alloc]initwithframe:effectview. Bounds];lable. Text= @"hahaha";lable. TextAlignment= Nstextalignmentcenter;lable. Font= [Uifont systemfontofsize: -];[Effectview. ContentviewAddsubview:lable];

As follows:

2. Add the Effectview in the sub-view to match the text with the background;

UIImage * image = [UIImage imagenamed:@"icon"];Uiimageview * ImageView = [[Uiimageview alloc]initwithframe:self. View. Bounds];ImageView. Image= Image;[Self. ViewAddsubview:imageview];Uiblureffect * blur = [Uiblureffect effectwithstyle:uiblureffectstylelight];Create Blur View Uivisualeffectview * Effectview = [[Uivisualeffectview Alloc]initwitheffect:blur];Effectview. Frame= CGRectMake (0, -, ImageView. Frame. Size. Width, $);[ImageView Addsubview:effectview];Add display text UILabel * lable = [[UILabel Alloc]initwithframe:effectview. Bounds];lable. Text= @"hahaha";lable. TextAlignment= Nstextalignmentcenter;lable. Font= [Uifont systemfontofsize: -];Create a Uivisualeffectview//of a fuzzy sub-view1.Create a sub-blurred view uivisualeffectview * Subeffectview = [[Uivisualeffectview alloc]initwitheffect:[uivibrancyeffect Effectforblureffect: (Uiblureffect *) Effectview. Effect]];//2.Set Size Subeffectview. Frame= Effectview. Bounds;//3.Adding a sub-fuzzy view to the Effectview Contentview will take effect [Effectview. ContentviewAddsubview:subeffectview];//4.Add a view to display to achieve a special effect [Subeffectview. ContentviewAddsubview:lable];

As follows:

4. Auto blur effect after downloading image

Import Uiimage+imageeffects and GCD files in the project;
Uiimage+imageeffects Address: Https://github.com/YouXianMing/UIImageBlur;
GCD Address: Https://github.com/YouXianMing/GCD-Program;

The implementation of the Code:

Create a UIView subclass, named Blurdownloadpicview;
Contents of BlurDownloadPicView.h:

#import <UIKit/UIKit.h>@interface BlurDownloadPicView : UIView//图片@property (nonatomicstrong)NSString * picUrl;//图片显示的方式@property (nonatomic)UIViewContentMode contentMode;//开始执行- (void)startProgress;@end

Contents of BLURDOWNLOADPICVIEW.M:

#import "BlurDownloadPicView.h" #import "GCD.h" #import "Uiimage+imageeffects.h"  @interface blurdownloadpicview ()@property(nonatomic,Strong)Uiimageview* ImageView;@end @implementation blurdownloadpicview -(Instancetype) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self) {        [ SelfLoadsubviews]; }return  Self;} - (void) loadsubviews{ Self. ImageView= [[UiimageviewAlloc]initwithframe: Self. Bounds]; Self. ImageView. Alpha=0; [ SelfAddsubview: Self. ImageView];} - (void) startprogress{if( Self. Picurl) {[Gcdqueue executeinglobalqueue:^{//Create request            nsurlrequest* Request = [nsurlrequestrequestwithurl:[NsurlURLWithString: Self. Picurl]];//Because it is a synchronous request, it blocks the main thread (mainqueue)NSData * data = [nsurlconnectionSendsynchronousrequest:request Returningresponse:NilErrorNil];//Get Pictures            UIImage* image = [[UIImageAlloc]initwithdata:data];//blur the image and block the main thread (mainqueue)            UIImage* Blurimage = [image blurimage]; [Gcdqueue executeinmainqueue:^{//Load Picture                 Self. ImageView. Image= Blurimage; [UIViewAnimatewithduration:1animations:^{ Self. ImageView. Alpha=1;            }];        }];    }]; }}//Override Set,get method-(void) Setcontentmode: (Uiviewcontentmode) contentmode{ Self. Contentmode= Contentmode; Self. ImageView. Contentmode= Contentmode;} -(Uiviewcontentmode) contentmode{return  Self. Contentmode;}

The realization within the VIEWCONTROLLER.M;
Introduction of #import "BlurDownloadPicView.h" header file

在viewDidLoad里面的代码实现- (void)viewDidLoad {    [super viewDidLoad];  NSString * picUrl = @"http://i4.download.fd.pchome.net/g1/M00/0D/1C/oYYBAFTEeAmIWRXgAAKhY5iKdP8AACP_QP4y0IAAqF7878.jpg";    BlurDownloadPicView * blurPicView = [[BlurDownloadPicView alloc]initWithFrame:self.view.bounds];    [self.view addSubview:blurPicView];    blurPicView.picUrl = picUrl;    [blurPicView startProgress];}

As follows:

IOS picture background Blur effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.