类似于支付宝双击home键进入后台模糊效果-blurView
2015-03-16 15:58
281 查看
废话少说直接代码
1.入口类里
#import "AppDelegate.h"
#import "JPBlurView.h"
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user
quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// [[NSNotificationCenter defaultCenter]postNotificationName:@"back" object:nil];
//双击进入后台
JPBlurView *view = [[JPBlurViewalloc]
initWithFrame:self.window.frame];
view.tag =1111111;
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
[window addSubview:view];
}
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated
later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
JPBlurView *view = [[JPBlurViewalloc]
initWithFrame:self.window.frame];
view.tag =1111111;
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
[window addSubview:view];
}
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//移除
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
UIView *view = [windowviewWithTag:1111111];
[view removeFromSuperview];
}
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//移除
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
UIView *view = [windowviewWithTag:1111111];
[view removeFromSuperview];
}
}
}
2.JPBlurView
JPBlurView.h
#import <UIKit/UIKit.h>
@interface JPBlurView :
UIView
@property(nonatomic,retain)UIImage *blurImage;
@end
JPBlurView.m
#import "JPBlurView.h"
#import <Accelerate/Accelerate.h>
#define screenWidth ([UIScreen mainScreen].bounds.size.width)
#define screenHeight ([UIScreen mainScreen].bounds.size.height)
#define screenScale ([UIScreen mainScreen].scale)
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
#define kCGImageAlphaPremultipliedLast (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else
#define kCGImageAlphaPremultipliedLast kCGImageAlphaPremultipliedLast
#endif
@implementation JPBlurView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
UIImage *image = [UIImageimageWithData:UIImageJPEGRepresentation([selfgetCurrentImage],
1.0)];
self.blurImage = [selfblurryImage:image
withBlurLevel:0.3];
UIImageView *bgView = [[UIImageViewalloc]
initWithFrame:frame];
bgView.image =self.blurImage;
[selfaddSubview:bgView];
}
returnself;
}
- (UIImage *)getCurrentImage
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(screenWidth*screenScale,screenHeight*screenScale),YES,
0);
//截屏
[[[[UIApplicationsharedApplication]
keyWindow]layer]
renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = viewImage.CGImage;
CGRect rect =CGRectMake(0,0,
screenWidth*screenScale,screenHeight*screenScale);
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImagealloc]
initWithCGImage:imageRefRect];
CGImageRelease(imageRefRect);
return sendImage;
}
//模糊算法
- (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 =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,kCGImageAlphaPremultipliedLast );
CGImageRef imageRef =CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImageimageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
@end
1.入口类里
#import "AppDelegate.h"
#import "JPBlurView.h"
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user
quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// [[NSNotificationCenter defaultCenter]postNotificationName:@"back" object:nil];
//双击进入后台
JPBlurView *view = [[JPBlurViewalloc]
initWithFrame:self.window.frame];
view.tag =1111111;
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
[window addSubview:view];
}
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated
later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
JPBlurView *view = [[JPBlurViewalloc]
initWithFrame:self.window.frame];
view.tag =1111111;
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
[window addSubview:view];
}
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//移除
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
UIView *view = [windowviewWithTag:1111111];
[view removeFromSuperview];
}
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//移除
for (UIWindow *windowin [[UIApplicationsharedApplication]
windows]) {
if (window.windowLevel ==UIWindowLevelNormal) {
UIView *view = [windowviewWithTag:1111111];
[view removeFromSuperview];
}
}
}
2.JPBlurView
JPBlurView.h
#import <UIKit/UIKit.h>
@interface JPBlurView :
UIView
@property(nonatomic,retain)UIImage *blurImage;
@end
JPBlurView.m
#import "JPBlurView.h"
#import <Accelerate/Accelerate.h>
#define screenWidth ([UIScreen mainScreen].bounds.size.width)
#define screenHeight ([UIScreen mainScreen].bounds.size.height)
#define screenScale ([UIScreen mainScreen].scale)
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
#define kCGImageAlphaPremultipliedLast (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else
#define kCGImageAlphaPremultipliedLast kCGImageAlphaPremultipliedLast
#endif
@implementation JPBlurView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
UIImage *image = [UIImageimageWithData:UIImageJPEGRepresentation([selfgetCurrentImage],
1.0)];
self.blurImage = [selfblurryImage:image
withBlurLevel:0.3];
UIImageView *bgView = [[UIImageViewalloc]
initWithFrame:frame];
bgView.image =self.blurImage;
[selfaddSubview:bgView];
}
returnself;
}
- (UIImage *)getCurrentImage
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(screenWidth*screenScale,screenHeight*screenScale),YES,
0);
//截屏
[[[[UIApplicationsharedApplication]
keyWindow]layer]
renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = viewImage.CGImage;
CGRect rect =CGRectMake(0,0,
screenWidth*screenScale,screenHeight*screenScale);
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImagealloc]
initWithCGImage:imageRefRect];
CGImageRelease(imageRefRect);
return sendImage;
}
//模糊算法
- (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 =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,kCGImageAlphaPremultipliedLast );
CGImageRef imageRef =CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImageimageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
@end
相关文章推荐
- iOS开发实现支付宝双击Home键进入后台后的毛玻璃效果
- Android学习之BlurImageView实现图像模糊效果
- iOS在后台情况下展现出模糊效果(双击home键,看到的应用程序模糊)
- 两种方法模仿支付宝进入到后台界面模糊
- iOS开发:避免MPMoviePlayerViewController播放完毕后自动dismiss,进入后台自动dismiss
- 避免MPMoviePlayerViewController播放完毕后自动dismiss,进入后台自动dismiss
- 定义Window进入和退出效果(及Window,Activity,View的理解)
- Android 为View实现双击效果
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- viewpager实现进入程序之前的欢迎界面效果
- Android游戏开发19:SurfaceView运行机制剖析--处理切换到后台再重新进入程序时的异常
- 19—(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理
- Android开发中使用WebView后程序进入后台依旧占用CPU的解决
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- ViewPager实现首次进入软件时左右滑屏的软件展示效果
- SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- 实现类似于wp的后台的登陆摇头效果和ajax提交验证的效果
- SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处
- Android SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!