iOS7 毛玻璃特效代码

原图:

效果图:

实现:
首先需要导入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.height <1){
NSLog(@"*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 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 < matrixSize;++i){
saturationMatrix[i]=(int16_t)roundf(floatingPointSaturationMatrix[i]* divisor);
}
if(hasBlur){
vImageMatrixMultiply_ARGB8888(&effectOutBuffer,&effectInBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
effectImageBuffersAreSwapped = YES;
}
else{
vImageMatrixMultiply_ARGB8888(&effectInBuffer,&effectOutBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
}
}
if(!effectImageBuffersAreSwapped)
effectImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(effectImageBuffersAreSwapped)
effectImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Set up output context.
UIGraphicsBeginImageContextWithOptions(self.size, NO,[[UIScreen mainScreen] scale]);
CGContextRef outputContext =UIGraphicsGetCurrentContext();
CGContextScaleCTM(outputContext,1.0,-1.0);
CGContextTranslateCTM(outputContext,0,-self.size.height);
// Draw base image.
CGContextDrawImage(outputContext, imageRect,self.CGImage);
// Draw effect image.
if(hasBlur){
CGContextSaveGState(outputContext);
if(maskImage){
CGContextClipToMask(outputContext, imageRect, maskImage.CGImage);
}
CGContextDrawImage(outputContext, imageRect, effectImage.CGImage);
CGContextRestoreGState(outputContext);
}
// Add in color tint.
if(tintColor){
CGContextSaveGState(outputContext);
CGContextSetFillColorWithColor(outputContext, tintColor.CGColor);
CGContextFillRect(outputContext, imageRect);
CGContextRestoreGState(outputContext);
}
// Output image is ready.
UIImage*outputImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
@end

调用:

代码如下:

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!

(0)

相关推荐

  • IOS检测指定路径的文件是否存在

    复制代码 代码如下: - (NSString *)dataPath:(NSString *)file  {      NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];      BOOL bo = [[NSFileManager defaultManager] creat

  • 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如 下: 复制代码 代码如下: System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control 对于iOS里的UI类,也有类似的继承关系. 例如对于UITextField,继承于UIControl:UIControl继承于UIView,

  • IOS开发之CocoaPods安装和使用教程

    这里只是简单写一下过程吧. 第一步:安装cocoapods sudo gem install cocoapods 要是不成功,执行以下操作 gem sources --remove https://rubygems.org/ gem sources -a http://ruby.taobao.org/ gem sources -l(用来查看是否成功) sudo gem install cocoapods 第二步:在项目中导入库 切到与工程目录相关的.xcodeproj同一目录下 vim Podf

  • 解析iOS内存不足时的警告以及处理过程

    内存警告 ios下每个app可用的内存是被限制的,如果一个app使用的内存超过了这个阀值,则系统会向该app发送Memory Warning消息.收到消息后,app必须尽可能多的释放一些不必要的内存,否则OS会关闭app. 几种内存警告级别(便于理解内存警告之后的行为)  Memory warning level: 复制代码 代码如下: typedef enum {                      OSMemoryNotificationLevelAny      = -1,     

  • 正则表达式在IOS中的应用及IOS中三种正则表达式的使用与比较

    正则表达式在ios中应用 一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式达到字符串的复杂控制. 二.正则表达式的语法 看一个过滤纯数字的例子 - (BOOL)validateNumber:(NSString *) textString { NSString* number=@"^[0-9]+$"; NSPredicate *numbe

  • iOS9苹果将原http协议改成了https协议的方法

    解决方法: 在info.plist 加入key <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 下面给大家介绍ios中http 和https 协议的访问 最近做个项目,开始采用的是HTTP协议实现客户端和服务器端的交互,后来需要改成HTTPS协议.在修改的过程中发现了一些问题,解决方案如下:

  • iOS开发中使用UILabel设置字体的相关技巧小结

    一.初始化 复制代码 代码如下: UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];      [self.view addSubview:myLabel]; 二.设置文字 1.设置默认文本 复制代码 代码如下: NSString *text = @"标签文本"; myLabel.text = text; 效果: 2.设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不

  • IOS中实现图片点击全屏预览

    如果你感觉累,那就对了那是因为你在走上坡路..这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发.有时想想感觉有的东西可以记录一下,就把它记录下来吧,这次想写一下关于单张图片点击全屏预览的问题,网上查了一些大神写的有的功能确实很强大但自己暂时想要的只是简单的功能就好,还有些方法自己也没弄出想要的效果,最后写了一个比较简单的点击单张图片的全屏预览和双指捏合缩小放大,可能有时要对图片做一些处理,这里放大后只是显示同一张图片并未做处理,下面直接贴出代码 // // ViewC

  • IOS等待时动画效果的实现

    查询时间或长或短,为了提升用户体验,目前用的比较多的手段之一就是查询等待时添加一个动态等待效果.当我们在请求网络时加载页面时有个动作效果,效果图如下: 源代码可以网上找开源项目Coding.net,上面的效果原理为两张图片组合,外面那个则为动画转动,里面的图标则是透明度的变化:主要代码如下: 1:把它封装在EaseLoadingView里面 @interface EaseLoadingView : UIView @property (strong, nonatomic) UIImageView

  • IOS 自定义UIPickView详解及实例代码

    IOS 自定义UIPickView 苹果一直推崇使用原生的组件,自带的UIPickView其实也很漂亮了,看起来也很美观.但是有时候,产品会有一些特殊的设计和需求.本文将会讲解如何修改苹果原生的组件的属性,达到自定义UIPickView的效果. 需求如下.需要自定义一个Tab.自定义选中文字的颜色.自定义选中颜色背景,自定义未选中文字颜色. 修改未选中的文字的字体和颜色 经过分析,上面的取消和确定按钮实现起来还是很简单的.加一个条就好了,我就不介绍具体步骤,下面的没有选中时候文字的样色,已经字体

随机推荐