原图:


效果图:

 实现:
首先需要导入Accelerate.framework。
然后把两个文件加入到自己的项目中即可。
UIImage ImageEffects.h

#import 

@interfaceUIImage(ImageEffects)

-(UIImage*)applyLightEffect;

-(UIImage*)applyExtraLightEffect;

-(UIImage*)applyDarkEffect;

-(UIImage*)applyTintEffectWithColor:(UIColor*)tintColor;

-(UIImage*)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor*)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage*)maskImage;

@end

UIImage ImageEffects.m

#import "UIImage ImageEffects.h"

#import 

#import 

@implementationUIImage(ImageEffects)

-(UIImage*)applyLightEffect

{

UIColor*tintColor =[UIColor colorWithWhite:1.0 alpha:0.3];

return[self applyBlurWithRadius:30 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyExtraLightEffect

{

UIColor*tintColor =[UIColor colorWithWhite:0.97 alpha:0.82];

return[self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyDarkEffect

{

UIColor*tintColor =[UIColor colorWithWhite:0.11 alpha:0.73];

return[self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyTintEffectWithColor:(UIColor*)tintColor

{

constCGFloatEffectColorAlpha=0.6;

UIColor*effectColor = tintColor;

int componentCount =CGColorGetNumberOfComponents(tintColor.CGColor);

if(componentCount ==2){

CGFloat b;

if([tintColor getWhite:&b alpha:NULL]){

effectColor =[UIColor colorWithWhite:b alpha:EffectColorAlpha];

}

}

else{

CGFloat r, g, b;

if([tintColor getRed:&r green:&g blue:&b alpha:NULL]){

effectColor =[UIColor colorWithRed:r green:g blue:b alpha:EffectColorAlpha];

}

}

return[self applyBlurWithRadius:10 tintColor:effectColor saturationDeltaFactor:-1.0 maskImage:nil];

}

-(UIImage*)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor*)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage*)maskImage

{

// Check pre-conditions.

if(self.size.width = 1: %@",self.size.width,self.size.height,self);

returnnil;

}

if(!self.CGImage){

NSLog(@"*** error: image must be backed by a CGImage: %@",self);

returnnil;

}

if(maskImage &&!maskImage.CGImage){

NSLog(@"*** error: maskImage must be backed by a CGImage: %@", maskImage);

returnnil;

}

CGRect imageRect ={CGPointZero,self.size };

UIImage*effectImage =self;

BOOL hasBlur = blurRadius > __FLT_EPSILON__;

BOOL hasSaturationChange = fabs(saturationDeltaFactor -1.)> __FLT_EPSILON__;

if(hasBlur || hasSaturationChange){

UIGraphicsBeginImageContextWithOptions(self.size, NO,[[UIScreen mainScreen] scale]);

CGContextRef effectInContext =UIGraphicsGetCurrentContext();

CGContextScaleCTM(effectInContext,1.0,-1.0);

CGContextTranslateCTM(effectInContext,0,-self.size.height);

CGContextDrawImage(effectInContext, imageRect,self.CGImage);

vImage_Buffer effectInBuffer;

effectInBuffer.data =CGBitmapContextGetData(effectInContext);

effectInBuffer.width =CGBitmapContextGetWidth(effectInContext);

effectInBuffer.height =CGBitmapContextGetHeight(effectInContext);

effectInBuffer.rowBytes =CGBitmapContextGetBytesPerRow(effectInContext);

UIGraphicsBeginImageContextWithOptions(self.size, NO,[[UIScreen mainScreen] scale]);

CGContextRef effectOutContext =UIGraphicsGetCurrentContext();

vImage_Buffer effectOutBuffer;

effectOutBuffer.data =CGBitmapContextGetData(effectOutContext);

effectOutBuffer.width =CGBitmapContextGetWidth(effectOutContext);

effectOutBuffer.height =CGBitmapContextGetHeight(effectOutContext);

effectOutBuffer.rowBytes =CGBitmapContextGetBytesPerRow(effectOutContext);

if(hasBlur){

// A description of how to compute the box kernel width from the Gaussian

// radius (aka standard deviation) appears in the SVG spec:

// http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement

// 

// For larger values of 's' (s >= 2.0), an approximation can be used: Three

// successive box-blurs build a piece-wise quadratic convolution kernel, which

// approximates the Gaussian kernel to within roughly 3%.

//

// let d = floor(s * 3*sqrt(2*pi)/4   0.5)

// 

// ... if d is odd, use three box-blurs of size 'd', centered on the output pixel.

// 

CGFloat inputRadius = blurRadius *[[UIScreen mainScreen] scale];

NSUInteger radius = floor(inputRadius *3.* sqrt(2* M_PI)/4 0.5);

if(radius %2!=1){

radius  =1;// force radius to be odd so that the three box-blur methodology works.

}

vImageBoxConvolve_ARGB8888(&effectInBuffer,&effectOutBuffer, NULL,0,0, radius, radius,0, kvImageEdgeExtend);

vImageBoxConvolve_ARGB8888(&effectOutBuffer,&effectInBuffer, NULL,0,0, radius, radius,0, kvImageEdgeExtend);

vImageBoxConvolve_ARGB8888(&effectInBuffer,&effectOutBuffer, NULL,0,0, radius, radius,0, kvImageEdgeExtend);

}

BOOL effectImageBuffersAreSwapped = NO;

if(hasSaturationChange){

CGFloat s = saturationDeltaFactor;

CGFloat floatingPointSaturationMatrix[]={

0.0722 0.9278* s,0.0722-0.0722* s,0.0722-0.0722* s,0,

0.7152-0.7152* s,0.7152 0.2848* s,0.7152-0.7152* s,0,

0.2126-0.2126* s,0.2126-0.2126* s,0.2126 0.7873* s,0,

0,0,0,1,

};

constint32_t divisor =256;

NSUInteger matrixSize =sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]);

int16_t saturationMatrix[matrixSize];

for(NSUInteger i =0; i 

调用:
 

UIImageView*me =[[UIImageView alloc] initWithFrame:CGRectMake(10,480,614,381)];

[me setImage:[[UIImage imageNamed:@"me.png"] applyBlurWithRadius:5 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.8 maskImage:nil]];

[self.view addSubview:me];

ok!So easy!

iOS7 毛玻璃特效代码的更多相关文章

  1. 【swift3.0】【毛玻璃效果】【简单粗暴的方法】

    贡献作者-【XJDomain】博客XJ:https://my.oschina.net/shengbingli/blogGitHub直播地址:https://github.com/lishengbing/XJDomainLive使用:方法:

  2. Swift 毛玻璃

    对于苹果用户,对于用户毛玻璃效果是很多用户喜欢的一个模式,但是对于开发者可就惨了,我说的是初级开发者,之前我开发毛玻璃,有三种方法,我只提前两者的思路,方法一:UIToolBar方法二:GPUImage方法三:废话少说,少说废话,上代码,简单的跟一似得!注意:下面这个方法是iOS8出来的!

  3. 用 CAShapeLayer、毛玻璃镂空效果创建加载动画

    让我们来看看如何将CALayer的spring动画和毛玻璃效果结合,创建好看的进度指示器。打开ViewController.swift添加3个类常量:声明一个UIView,用于添加到viewcontroller,以及一个CAShapeLayer用于显示弧形。关于CAShapeLayer的属性和动画,请参考《iOSAnimationsbyTutorials》第13章“形状和遮罩”以及第15章“笔触和路径动画”。Build&run,你会看到:很好——你可以对它使用spring动画并让CAShapeLayer动

  4. ios7中UIViewControllerBasedStatusBarAppearance作用详解

    这篇文章主要介绍了 ios7中UIViewControllerBasedStatusBarAppearance作用详解的相关资料,需要的朋友可以参考下

  5. Android实现毛玻璃效果弹出菜单动画

    这篇文章主要为大家详细介绍了Android实现毛玻璃效果弹出菜单动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  6. Android开发Activity毛玻璃背景效果

    这篇文章主要为大家详细介绍了Android开发Activity毛玻璃背景效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  7. IOS图片设置毛玻璃效果

    这篇文章主要介绍了IOS图片设置毛玻璃效果的相关资料,需要的朋友可以参考下

  8. iOS 8使用UIBlurEffect实现毛玻璃特效

    这篇文章主要为大家详细介绍了iOS 8使用UIBlurEffect类和UIVisualEffectView类实现毛玻璃特效,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. iOS7 毛玻璃特效代码

    这篇文章主要分享了iOS7 毛玻璃特效代码,非常的实用,做IOS开发的童鞋们不要错过了

  10. iOS毛玻璃效果的实现及图片模糊效果的三种方法

    App设计时往往会用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以让我们方便是使用,本文给大家介绍iOS毛玻璃效果的实现及图片模糊效果的三种方法,感兴趣的朋友一起学习吧

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部