本文为大家分享了ios图片模糊效果的三种实现方式,供大家参考,具体内容如下
1.实现效果依次如图:原图、ios8效果、core image效果、 vimage 效果
-
2. 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#import "viewcontroller.h" #import <accelerate/accelerate.h> @interface viewcontroller () @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@ "background" ]]; // [self ios8blurimageimplement]; // [self coreimageimplement]; [self vimageimplement]; } // ios8 使用系统自带的处理方式 - ( void )ios8blurimageimplement { uiblureffect *beffect = [uiblureffect effectwithstyle:uiblureffectstylelight]; uivisualeffectview *view = [[uivisualeffectview alloc] initwitheffect:beffect]; view.frame = self.view.bounds; [self.view addsubview:view]; } // 使用coreimage实现图片模糊 - ( void )coreimageimplement{ cicontext *context = [cicontext contextwithoptions:nil]; nserror *error = nil; nsstring *filepath = [[nsbundle mainbundle] pathforresource:@ "background" oftype:@ "png" ]; nsdata *imagedata = [nsdata datawithcontentsoffile:filepath options:nsdatareadinguncached error:&error]; //nsdata *imagedata = [nsdata datawithcontentsoffile:@"background.png"]; ciimage *image = [ciimage imagewithdata:imagedata]; cifilter *filter = [cifilter filterwithname:@ "cigaussianblur" ]; [filter setvalue:image forkey:kciinputimagekey]; [filter setvalue:@2.0f forkey:@ "inputradius" ]; ciimage *result = [filter valueforkey:kcioutputimagekey]; cgimageref outimage = [context createcgimage:result fromrect:[result extent]]; uiimage *bluerimage = [uiimage imagewithcgimage:outimage]; uiimageview *imageview = [[uiimageview alloc] initwithimage:bluerimage]; imageview.frame = self.view.bounds; [self.view addsubview:imageview]; } // 使用vimage api实现图片模糊 // ios5.0中新增了vimage api可以使用,它属于accelerate.framework,所以如果你要使用它要在工程中加入这个framework。模糊算法使用的是vimageboxconvolve_argb8888这个函数。 - ( void )vimageimplement { uiimage *image = [uiimage imagenamed:@ "background" ]; uiimage *blurimage = [self blurryimage:image withblurlevel:0.5]; self.view.backgroundcolor = [uicolor colorwithpatternimage:blurimage]; } - (uiimage *)blurryimage:(uiimage *)image withblurlevel:(cgfloat)blur { if (blur < 0.f || blur > 1.f) { blur = 0.5f; } int boxsize = ( int )(blur * 100); boxsize = boxsize - (boxsize % 2) + 1; cgimageref img = image.cgimage; vimage_buffer inbuffer, outbuffer; vimage_error error; voidvoid *pixelbuffer; cgdataproviderref inprovider = cgimagegetdataprovider(img); cfdataref inbitmapdata = cgdataprovidercopydata(inprovider); inbuffer.width = cgimagegetwidth(img); inbuffer.height = cgimagegetheight(img); inbuffer.rowbytes = cgimagegetbytesperrow(img); inbuffer.data = ( void *)cfdatagetbyteptr(inbitmapdata); pixelbuffer = malloc (cgimagegetbytesperrow(img) * cgimagegetheight(img)); if (pixelbuffer == null) nslog(@ "no pixelbuffer" ); outbuffer.data = pixelbuffer; outbuffer.width = cgimagegetwidth(img); outbuffer.height = cgimagegetheight(img); outbuffer.rowbytes = cgimagegetbytesperrow(img); error = vimageboxconvolve_argb8888(&inbuffer, &outbuffer, null, 0, 0, boxsize, boxsize, null, kvimageedgeextend); if (error) { nslog(@ "error from convolution %ld" , error); } cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cgcontextref ctx = cgbitmapcontextcreate( outbuffer.data, outbuffer.width, outbuffer.height, 8, outbuffer.rowbytes, colorspace, kcgimagealphanoneskiplast); cgimageref imageref = cgbitmapcontextcreateimage (ctx); uiimage *returnimage = [uiimage imagewithcgimage:imageref]; //clean up cgcontextrelease(ctx); cgcolorspacerelease(colorspace); free (pixelbuffer); cfrelease(inbitmapdata); cgcolorspacerelease(colorspace); cgimagerelease(imageref); return returnimage; } @end |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。