工欲善其事,必先利其器系列之 UIAlertView+Block

UIAlertView是消息提示框UI控件,对于消息提示框的中的按钮事件采用的是事件委托机制。要实现事件响应,需要实现对应协议、重写函数达到目的。

本文介绍
1、UIAlertView+Block,简化代码
2、自定义Alert+Block 简单的自定义弹框封装

正常写法

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 
                                                  message:@"这是一个警告框!" 
                                                  delegate:self  
                                                  cancelButtonTitle:@"确定" 
                                                  otherButtonTitles:nil];  
 [alert show]; 



//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"点击的按钮是第%ld个",buttonIndex);
}

能不能偷懒?那就试试自定义Category

UIAlertView+Block

#import <UIKit/UIKit.h>

//点以一个点击回调Block
typedef void(^SmileAlertClickedBlock)(NSInteger buttonIndex);
@interface SmileAlert : UIView
@property (nonatomic, copy) SmileAlertClickedBlock alertViewCallBackBlock;
+ (void)creat_AlertViewWithClickBlock:(SmileAlertClickedBlock)alertViewClickBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonStr:(NSString *)cancelButtonStr otherButtonTitle:(NSString *)otherButtonTitle;

@end

UIAlertView+Block.m

#import "UIAlertView+Block.h"
#import <objc/runtime.h>

static NSString *UIAlertViewKey = @"UIAlertViewKey";

@implementation UIAlertView (Block)

+ (void)creat_AlertViewWithClickBlock:(UIAlertViewClickedBlock)alertViewClickBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonStr:(NSString *)cancelButtonStr otherButtonTitles:(NSString *)otherButtonTitles, ...NS_REQUIRES_NIL_TERMINATION{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonStr otherButtonTitles: otherButtonTitles, nil];
    NSString *other = nil;
    va_list args;
    if (otherButtonTitles) {
        va_start(args, otherButtonTitles);
        while ((other = va_arg(args, NSString*))) {
            [alert addButtonWithTitle:other];
        }
        va_end(args);
    }
    alert.delegate = alert;
    [alert show];
    alert.alertViewCallBackBlock = alertViewClickBackBlock;
    
}

- (void)setAlertViewCallBackBlock:(UIAlertViewClickedBlock)alertViewCallBackBlock {
    
    [self willChangeValueForKey:@"callbackBlock"];
    objc_setAssociatedObject(self, &UIAlertViewKey, alertViewCallBackBlock, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"callbackBlock"];
}

- (UIAlertViewClickedBlock)alertViewCallBackBlock {
    
    return objc_getAssociatedObject(self, &UIAlertViewKey);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (self.alertViewCallBackBlock) {
        self.alertViewCallBackBlock(buttonIndex);
    }
}


@end


调用

    [UIAlertView creat_AlertViewWithClickBlock:^(NSInteger buttonIndex) {
        NSLog(@"点击的按钮是第%ld个",buttonIndex);
    } title:@"标题" message:@"今天天气不错!!💗" cancelButtonStr:@"呵呵" otherButtonTitles:@"嗯嗯",@"思密达", nil];

效果:

AE7282E5-1E57-4709-A38F-D108BC8B0482.png

]


二、简单的自定义弹框SmileAlert

和UIAlertView+Block思路一样,主要是通过Block回传点击索引,代码简单,直接贴上:

SmileAlert.h

#import <UIKit/UIKit.h>
//点以一个点击回调Block
typedef void(^SmileAlertClickedBlock)(NSInteger buttonIndex);

@interface SmileAlert : UIView

@property (nonatomic, copy) SmileAlertClickedBlock alertViewCallBackBlock;

+ (void)creat_AlertViewWithClickBlock:(SmileAlertClickedBlock)alertViewClickBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonStr:(NSString *)cancelButtonStr otherButtonTitle:(NSString *)otherButtonTitle;


@end

SmileAlert.m

//
//  SmileAlert.m
//  SmileHelper
//
//  Created by 微笑吧阳光 on 2016/2/28.
//  Copyright © 2016年 www.imee.vc. All rights reserved.
//

#import "SmileAlert.h"
#import <Accelerate/Accelerate.h>
#import "AppDelegate.h"
#import "Macros.h"
#import "SmileAlertContentView.h"

#define kAppDelegate        (AppDelegate *)[[UIApplication sharedApplication] delegate]

#define KiOS7OrLater ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)

@interface SmileAlert()

@property (nonatomic, strong) UIImageView *screenShotView;
@property (nonatomic, strong) UIImageView *alertPopView;

@property (nonatomic, copy) NSString *titleStr;
@property (nonatomic, copy) NSString *messageStr;
@property (nonatomic, copy) NSString *cancelBtnStr;
@property (nonatomic, copy) NSString *sureBtnStr;

@end

@implementation SmileAlert

+ (void)creat_AlertViewWithClickBlock:(SmileAlertClickedBlock)alertViewClickBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonStr:(NSString *)cancelButtonStr otherButtonTitle:(NSString *)otherButtonTitle{
    
    SmileAlert * alert = [[SmileAlert alloc]initWithTitle:title message:message cancelButtonStr:cancelButtonStr otherButtonTitle:otherButtonTitle AndClickBlock:alertViewClickBackBlock];
    [alert showAlert];
}

-(instancetype)initWithTitle:(NSString*)title  message:(NSString *)message  cancelButtonStr:(NSString *)cancelButtonStr otherButtonTitle:(NSString *)otherButtonTitle AndClickBlock:(SmileAlertClickedBlock)alertViewClickBackBlock
{
    self=[super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
    self.titleStr = title;
    self.messageStr = message;
    self.cancelBtnStr = cancelButtonStr;
    self.sureBtnStr = otherButtonTitle;
    [self creatUi];
    self.alertViewCallBackBlock = alertViewClickBackBlock;
    }
    return self;
}

-(void)creatUi{
    
    [self addScreenShot];
    [self addPopAlertView];
    
}

-(void)addPopAlertView{
    
    SmileAlertContentView * contentView = [[[NSBundle mainBundle]loadNibNamed:@"SmileAlertContentView" owner:self options:nil]lastObject];
    contentView.frame = CGRectMake(0, 0, self.alertPopView.frame.size.width, self.alertPopView.frame.size.height);
    contentView.center = CGPointMake(self.alertPopView.frame.size.width/2, self.alertPopView.frame.size.height/2);
    contentView.titleStr.text = self.titleStr;
    contentView.contentStr.text = self.messageStr;
    [contentView.cancelBtn setTitle:self.cancelBtnStr forState:UIControlStateNormal];
    [contentView.sureBtn setTitle:self.sureBtnStr forState:UIControlStateNormal];
    
    [contentView returnbuttonIndex:^(NSInteger buttonIndex) {
//        NSLog(@"💗💗----%ld",(long)buttonIndex);
        if (self.alertViewCallBackBlock != nil) {
             self.alertViewCallBackBlock(buttonIndex);
        }
        [self hideAlert];
    }];
    [self.alertPopView addSubview:contentView];
    [self addSubview:self.alertPopView];
}

#pragma mark - 出来吧,弹出框
- (void)showAlert{

    AppDelegate*app=kAppDelegate;
    [app.window addSubview:self];
    self.backgroundColor = [UIColor blackColor];
    CGFloat duration = 0.3;
    
//    for (UIButton *btn in self.alertView.subviews) {
//        btn.userInteractionEnabled = NO;
//    }
    
    self.alertPopView.alpha = 0;
    self.alertPopView.alpha = 0;
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.screenShotView.alpha = 1;
        self.alertPopView.alpha = 1.0;
    } completion:^(BOOL finished) {
        for (UIButton *btn in self.subviews) {
            btn.userInteractionEnabled = YES;
        }
    }];
    
    if (KiOS7OrLater) {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        animation.values = @[@(0.8), @(1.05), @(1.1), @(1)];
        animation.keyTimes = @[@(0), @(0.3), @(0.5), @(1.0)];
        animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
        animation.duration = duration;
        [self.alertPopView.layer addAnimation:animation forKey:@"bouce"];
    } else {
        self.alertPopView.transform = CGAffineTransformMakeScale(0.8, 0.8);
        [UIView animateWithDuration:duration * 0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.alertPopView.transform = CGAffineTransformMakeScale(1.05, 1.05);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:duration * 0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
               self.alertPopView.transform = CGAffineTransformMakeScale(1.1, 1.1);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:duration * 0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                  self.alertPopView.transform = CGAffineTransformMakeScale(1, 1);
                } completion:nil];
            }];
        }];
    }
}

//消失吧,提示框
- (void)hideAlert{
    
    CGFloat duration = 0.2;
    
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.screenShotView.alpha = 0;
        self.alertPopView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.screenShotView removeFromSuperview];
        [self removeFromSuperview];
     
    }];
    
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.alertPopView.transform = CGAffineTransformMakeScale(0.4, 0.4);
    } completion:^(BOOL finished) {
        self.alertPopView.transform = CGAffineTransformMakeScale(1, 1);
    }];

}

//添加一个模糊的效果
- (void)addScreenShot{
    UIWindow *screenWindow = [UIApplication sharedApplication].windows.firstObject;
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImage *originalImage = nil;
    if (KiOS7OrLater) {
        originalImage = viewImage;
    } else {
        originalImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(viewImage.CGImage, CGRectMake(0, 20, 320, 460))];
    }
    
    CGFloat blurRadius = 4;
    UIColor *tintColor = [UIColor clearColor];
    CGFloat saturationDeltaFactor = 1;
    UIImage *maskImage = nil;
    
    CGRect imageRect = { CGPointZero, originalImage.size };
    UIImage *effectImage = originalImage;
    
    BOOL hasBlur = blurRadius > __FLT_EPSILON__;
    BOOL hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__;
    if (hasBlur || hasSaturationChange) {
        UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, [[UIScreen mainScreen] scale]);
        CGContextRef effectInContext = UIGraphicsGetCurrentContext();
        CGContextScaleCTM(effectInContext, 1.0, -1.0);
        CGContextTranslateCTM(effectInContext, 0, -originalImage.size.height);
        CGContextDrawImage(effectInContext, imageRect, originalImage.CGImage);
        
        vImage_Buffer effectInBuffer;
        effectInBuffer.data  = CGBitmapContextGetData(effectInContext);
        effectInBuffer.width    = CGBitmapContextGetWidth(effectInContext);
        effectInBuffer.height   = CGBitmapContextGetHeight(effectInContext);
        effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext);
        
        UIGraphicsBeginImageContextWithOptions(originalImage.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];
            uint32_t radius = floor(inputRadius * 3. * sqrt(2 * M_PI) / 4 + 0.5);
            if (radius % 2 != 1) {
                radius += 1;
            }
            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,
            };
            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(originalImage.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef outputContext = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(outputContext, 1.0, -1.0);
    CGContextTranslateCTM(outputContext, 0, -originalImage.size.height);
    
    CGContextDrawImage(outputContext, imageRect, originalImage.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);
    }
    
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.screenShotView = [[UIImageView alloc] initWithImage:outputImage];
    self.screenShotView.frame =[UIScreen mainScreen].bounds;
    [self addSubview:self.screenShotView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [self.screenShotView addGestureRecognizer:tap];

}
#pragma mark - 点击事件
- (void)dismiss:(UITapGestureRecognizer *)tap {
    [self hideAlert];
}

#pragma mark 懒加载初始化
- (UIImageView *)alertPopView {
    if (_alertPopView == nil) {
//        _alertPopView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"share_分享背景"]];
        _alertPopView = [[UIImageView alloc]initWithImage:nil];
        _alertPopView.backgroundColor = [UIColor whiteColor];
        _alertPopView.frame = CGRectMake(0, 0, KScreenWidth-60, KScreenHeight/4);
        _alertPopView.center = CGPointMake( KScreenWidth/ 2, KScreenHeight / 2);
        ViewRadius(_alertPopView, 5);
    }
    return _alertPopView;
}



@end


其中SmileAlertContentView为一个xib文件,代码就不贴了,需要的可下载源码看下

调用

   [SmileAlert creat_AlertViewWithClickBlock:^(NSInteger buttonIndex) {
        
         NSLog(@"SmileAlert点击 第%ld个",buttonIndex);
        
    } title:@"SmileAlert" message:@"分层是表示将功能进行有序的分组:应用程序专用功能位于上层,跨越应用程序领域的功能位于中层,而配置环境专用功能位于低层。分层从逻辑上将子系统划分成许多集合,而层间关系的形成要遵循一定的规则。"
                              cancelButtonStr:@"🐘取消"
                              otherButtonTitle:@"💗确定"];

效果

027CDEA9-E376-43C3-A264-A00125A84363.png

⬇️⬇️⬇️特别指出的一段代码是添加模糊效果的背景 ⬇️⬇️⬇️⬇️⬇️⬇️

//添加一个模糊的效果
- (void)addScreenShot{
    UIWindow *screenWindow = [UIApplication sharedApplication].windows.firstObject;
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImage *originalImage = nil;
    if (KiOS7OrLater) {
        originalImage = viewImage;
    } else {
        originalImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(viewImage.CGImage, CGRectMake(0, 20, 320, 460))];
    }
    
    CGFloat blurRadius = 4;
    UIColor *tintColor = [UIColor clearColor];
    CGFloat saturationDeltaFactor = 1;
    UIImage *maskImage = nil;
    
    CGRect imageRect = { CGPointZero, originalImage.size };
    UIImage *effectImage = originalImage;
    
    BOOL hasBlur = blurRadius > __FLT_EPSILON__;
    BOOL hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__;
    if (hasBlur || hasSaturationChange) {
        UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, [[UIScreen mainScreen] scale]);
        CGContextRef effectInContext = UIGraphicsGetCurrentContext();
        CGContextScaleCTM(effectInContext, 1.0, -1.0);
        CGContextTranslateCTM(effectInContext, 0, -originalImage.size.height);
        CGContextDrawImage(effectInContext, imageRect, originalImage.CGImage);
        
        vImage_Buffer effectInBuffer;
        effectInBuffer.data  = CGBitmapContextGetData(effectInContext);
        effectInBuffer.width    = CGBitmapContextGetWidth(effectInContext);
        effectInBuffer.height   = CGBitmapContextGetHeight(effectInContext);
        effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext);
        
        UIGraphicsBeginImageContextWithOptions(originalImage.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];
            uint32_t radius = floor(inputRadius * 3. * sqrt(2 * M_PI) / 4 + 0.5);
            if (radius % 2 != 1) {
                radius += 1;
            }
            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,
            };
            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(originalImage.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef outputContext = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(outputContext, 1.0, -1.0);
    CGContextTranslateCTM(outputContext, 0, -originalImage.size.height);
    
    CGContextDrawImage(outputContext, imageRect, originalImage.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);
    }
    
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.screenShotView = [[UIImageView alloc] initWithImage:outputImage];
    self.screenShotView.frame =[UIScreen mainScreen].bounds;
    [self addSubview:self.screenShotView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [self.screenShotView addGestureRecognizer:tap];

}

项目传送门
项目地址
https://github.com/SmileMee/SmileHelper.git


我是写代码的凡,如有错误,欢迎指正!🙂🙂🙂

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,718评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,683评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,207评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,755评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,862评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,050评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,136评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,882评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,330评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,651评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,789评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,477评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,135评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,864评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,099评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,598评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,697评论 2 351

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,066评论 4 62
  • 查阅 DAY1 - DAY6 的同学优秀作业(助教稍后会整理提供),写出自己具体学到了哪些以及哪些可以改进的地方。...
    王小锤子阅读 364评论 0 0
  • 今天周六,舍友去别的矿找同学了,我想自己安静休息会儿就婉拒了舍友的邀请。 真当我一个人时,内心并不安宁,小人闲居为...
    生命的朝拜者阅读 164评论 0 0
  • 我知道我有点任性 我知道我有点调皮 我知道我有一点点的傻 但是我也知道 不论我是什么样子 你们都会是最爱我的 我最...
    云清一阅读 206评论 3 6
  • 阳光冷 清秋月 古树长藤 蛛网蔽阙 一夕长空云朦朦 半宿飞叶风孑孓 长剑寒 飞花乱 别姬霸王 雪残桥断 三生酒醒梦...
    茗香酒影阅读 162评论 2 3