最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法

 更新時(shí)間:2016年01月15日 10:31:41   作者:Arvin~  
App設(shè)計(jì)時(shí)往往會(huì)用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以讓我們方便是使用,本文給大家介紹iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法,感興趣的朋友一起學(xué)習(xí)吧

App設(shè)計(jì)時(shí)往往會(huì)用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以讓我們方便是使用。

話說蘋果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,比如下圖的通知中心界面;


但是其iOS7.0的SDK并沒有提供給開發(fā)者實(shí)現(xiàn)毛玻璃效果的API,所以很多人都是通過一些別人封裝的框架來實(shí)現(xiàn),后面我也會(huì)講到一個(gè);

其實(shí)在iOS7.0(包括)之前還是有系統(tǒng)的類可以實(shí)現(xiàn)毛玻璃效果的, 就是 UIToolbar這個(gè)類,并且使用相當(dāng)簡單,幾行代碼就可以搞定.

下面是代碼實(shí)現(xiàn):

創(chuàng)建一個(gè)UIToolbar實(shí)例,設(shè)置它的frame或者也可以通過添加約束

然后UIToolbar有一個(gè)屬性:barStyle,設(shè)置對應(yīng)的枚舉值來呈現(xiàn)毛玻璃的樣式,最后再添加到需要進(jìn)行毛玻璃效果的view上即可.

/*
毛玻璃的樣式(枚舉)
UIBarStyleDefault = ,
UIBarStyleBlack = ,
UIBarStyleBlackOpaque = , // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = , // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
*/
UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
bgImgView.image = [UIImage imageNamed:@"huoying.jpg"];
[self.view addSubview:bgImgView];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(, , bgImgView.frame.size.width*., bgImgView.frame.size.height)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[bgImgView addSubview:toolbar]; 

效果圖:


我們再來看看視圖結(jié)構(gòu):

通過視圖結(jié)構(gòu)可以看到UIToolbar包含了三個(gè)子視圖

一個(gè)背景圖片和1個(gè)背景view,還有1個(gè)背景特效view,正是這幾個(gè)視圖結(jié)合在一起實(shí)現(xiàn)了毛玻璃的效果

在iOS8.0之后,蘋果新增了一個(gè)類UIVisualEffectView,通過這個(gè)類來實(shí)現(xiàn)毛玻璃效果與上面的UIToolbar一樣,而且效率也非常之高,使用也是非常簡單,幾行代碼搞定. UIVisualEffectView是一個(gè)抽象類,不能直接使用,需通過它下面的三個(gè)子類來實(shí)現(xiàn)(UIBlurEffect, UIVisualEffevt, UIVisualEffectView);

子類UIBlurEffect只有一個(gè)類方法,用來快速創(chuàng)建一個(gè)毛玻璃效果,參數(shù)是一個(gè)枚舉,用來設(shè)置毛玻璃的樣式,而UIVisualEffectView則多了兩個(gè)屬性和兩個(gè)構(gòu)造方法,用來快速將創(chuàng)建的毛玻璃添加到這個(gè)UIVisualEffectView上.

特別注意: 這個(gè)類是iOS8.0之后才適用, 所以如果項(xiàng)目要兼容iOS7.0的話, 還是要考慮其它的兩種方法了.

下面來看看實(shí)現(xiàn)代碼:

同樣是先快速的實(shí)例化UIBlurEffect并設(shè)置毛玻璃的樣式,然后再通過UIVisualEffectView的構(gòu)造方法將UIBlurEffect的實(shí)例添加上去最后設(shè)置frame或者是通過添加約束, 將effectView添加到要實(shí)現(xiàn)了毛玻璃的效果的view控件上,效果圖和上面的一樣.

UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
bgImgView.image = [UIImage imageNamed:@"huoying.jpg"];
bgImgView.contentMode = UIViewContentModeScaleAspectFill;
//[bgImgView setImageToBlur: [UIImage imageNamed:@"huoying.jpg"] blurRadius: completionBlock:nil];
bgImgView.userInteractionEnabled = YES;
[self.view addSubview:bgImgView];
/*
毛玻璃的樣式(枚舉)
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark
*/
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = CGRectMake(, , bgImgView.frame.size.width*., bgImgView.frame.size.height);
[bgImgView addSubview:effectView]; 

但是我們來看看視圖結(jié)構(gòu),大家會(huì)發(fā)現(xiàn)和Toolbar不一樣哦!

其實(shí)是因?yàn)閁IVisualEffectView這個(gè)類,構(gòu)造方法幫我們創(chuàng)建了一個(gè)view,而這個(gè)view我們給它做了毛玻璃處理,再將其覆蓋到了背景圖之上

嗯! 最后再來給大家介紹一個(gè)國外大神封裝的UIImageView的分類,里面不管是怎么實(shí)現(xiàn)的,反正使用非常簡單,只要一句代碼就搞定.

下面先看代碼:

UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//bgImgView.image = [UIImage imageNamed:@"huoying.jpg"];
bgImgView.contentMode = UIViewContentModeScaleAspectFill;
// 對背景圖片進(jìn)行毛玻璃效果處理 參數(shù)blurRadius默認(rèn)是,可指定,最后一個(gè)參數(shù)block回調(diào)可以為nil
[bgImgView setImageToBlur: [UIImage imageNamed:@"huoying.jpg"] blurRadius: completionBlock:nil];
bgImgView.userInteractionEnabled = YES;
[self.view addSubview:bgImgView]; 

效果圖:


再來看看添加毛玻璃效果后的視圖結(jié)構(gòu):

哈哈哈, 大家應(yīng)該看懂了, 這是直接對背景圖片進(jìn)行了高斯模糊處理了,其它就不解釋了.

好啦, 反正iOS中要進(jìn)行毛玻璃效果處理就這幾種方式,看大家的需求,喜歡用哪種就用哪種吧.

上面的demo,包括大神封裝的分類,如果需要詳細(xì)的源代碼的話,可以到我的gitHub上Clone啦!有問題歡迎留言一起探討學(xué)習(xí).

下面給大家介紹圖片模糊效果的三種方法

第一種使用Core Image進(jìn)行模糊

- (UIImage *)blurryImage:(UIImage *)image 
withBlurLevel:(CGFloat)blur { 
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage]; 
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" 
keysAndValues:kCIInputImageKey, inputImage, 
@"inputRadius", @(blur), 
]; CIImage *outputImage = filter.outputImage; 
CGImageRef outImage = [self.context createCGImage:outputImage 
fromRect:[outputImage extent]]; 
return [UIImage imageWithCGImage:outImage]; }

第二種使用vImage API進(jìn)行模糊

- (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; 
void *pixelBuffer; 
CGDataProviderRef inProvider = CGImageGetDataProvider(img); 
CFDataRef inBitmapData = http://www.open-open.com/code/view/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; } 

第三種方法是網(wǎng)上找到的(毛玻璃效果)

// 內(nèi)部方法,核心代碼,封裝了毛玻璃效果 參數(shù):半徑,顏色,色彩飽和度- (UIImage *)imageBluredWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage { 
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 = http://www.open-open.com/code/view/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) { 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, (short)radius, (short)radius, 0, kvImageEdgeExtend); vImageBoxConvolve_ARGB8888(&effectOutBuffer, &effectInBuffer, NULL, 0, 0, (short)radius, (short)radius, 0, kvImageEdgeExtend); vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (short)radius, (short)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, 
}; 
const int32_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(); 
} 
// 開啟上下文 用于輸出圖像 
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]); 
CGContextRef outputContext = UIGraphicsGetCurrentContext(); 
CGContextScaleCTM(outputContext, 1.0, -1.0); 
CGContextTranslateCTM(outputContext, 0, -self.size.height); 
// 開始畫底圖 CGContextDrawImage(outputContext, imageRect, self.CGImage); 
// 開始畫模糊效果 
if (hasBlur)
{ 
CGContextSaveGState(outputContext); 
if (maskImage) 
{ 
CGContextClipToMask(outputContext, imageRect, maskImage.CGImage); 
} CGContextDrawImage(outputContext, imageRect, effectImage.CGImage); 
CGContextRestoreGState(outputContext); 
} 
// 添加顏色渲染 
if (tintColor)
{ 
CGContextSaveGState(outputContext); 
CGContextSetFillColorWithColor(outputContext, tintColor.CGColor); 
CGContextFillRect(outputContext, imageRect); 
CGContextRestoreGState(outputContext); 
} 
// 輸出成品,并關(guān)閉上下文 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return outputImage;}

相關(guān)文章

  • 解決Charles抓包https時(shí),無法查看CONNECT請求的問題

    解決Charles抓包https時(shí),無法查看CONNECT請求的問題

    下面小編就為大家分享一篇解決Charles抓包https時(shí),無法查看CONNECT請求的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS自定義相機(jī)功能

    iOS自定義相機(jī)功能

    這篇文章主要為大家詳細(xì)介紹了iOS自定義相機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • iOS App開發(fā)中導(dǎo)航欄的創(chuàng)建及基本屬性設(shè)置教程

    iOS App開發(fā)中導(dǎo)航欄的創(chuàng)建及基本屬性設(shè)置教程

    這篇文章主要介紹了iOS App開發(fā)中導(dǎo)航欄的創(chuàng)建及基本屬性設(shè)置教程,即用UINavigationController來編寫navigation,示例代碼為Objective-C語言,需要的朋友可以參考下
    2016-02-02
  • iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼

    iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)步驟進(jìn)度條功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新)

    詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新)

    這篇文章主要介紹了詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • 詳解Objective-C中的語法糖@{}究竟是什么

    詳解Objective-C中的語法糖@{}究竟是什么

    這篇文章主要給大家介紹了關(guān)于Objective-C中語法糖@{}究竟是什么的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 仿iOS圖標(biāo)抖動(dòng)

    仿iOS圖標(biāo)抖動(dòng)

    最近閑來無聊,研究了一下IPhone桌面圖標(biāo)的抖動(dòng),網(wǎng)上有一個(gè)類似的事例,但是我看來效果實(shí)在不佳,自己也來寫一個(gè)玩玩,有需要的小伙伴可以參考下。
    2015-05-05
  • iOS?StoreKit?2?新特性盤點(diǎn)解析

    iOS?StoreKit?2?新特性盤點(diǎn)解析

    這篇文章主要為大家介紹了iOS?StoreKit?2?新特性盤點(diǎn)及要點(diǎn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS 鍵盤輸入限制(只能輸入字母,數(shù)字,禁止輸入特殊符號)

    iOS 鍵盤輸入限制(只能輸入字母,數(shù)字,禁止輸入特殊符號)

    本文主要介紹了iOS中鍵盤輸入限制(只能輸入字母,數(shù)字,禁止輸入特殊符號)的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • Objective-C關(guān)鍵字@property使用原理探究

    Objective-C關(guān)鍵字@property使用原理探究

    這篇文章主要為大家介紹了Objective-C關(guān)鍵字@property使用原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評論

咸宁市| 九江市| 弥勒县| 辽宁省| 永康市| 丹阳市| 扶绥县| 平泉县| 甘谷县| 沈阳市| 夏河县| 丹东市| 准格尔旗| 南江县| 济源市| 大姚县| 射洪县| 罗城| 连州市| 兴宁市| 赤水市| 上饶市| 通榆县| 镇雄县| 垫江县| 贵南县| 红桥区| 临武县| 乌海市| 边坝县| 桐庐县| 武威市| 巴彦淖尔市| 嘉义市| 天等县| 崇礼县| 湖北省| 玉环县| 连州市| 府谷县| 遂溪县|