欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS图片模糊效果的实现方法

程序员文章站 2023-12-17 21:21:04
本文为大家分享了ios图片模糊效果的三种实现方式,供大家参考,具体内容如下 1.实现效果依次如图:原图、ios8效果、core image效果、 vimage 效果 -...

本文为大家分享了ios图片模糊效果的三种实现方式,供大家参考,具体内容如下

1.实现效果依次如图:原图、ios8效果、core image效果、 vimage 效果

iOS图片模糊效果的实现方法-iOS图片模糊效果的实现方法iOS图片模糊效果的实现方法iOS图片模糊效果的实现方法

2. 代码

#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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: