奔跑吧 iOS

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

2017-02-28  本文已影响23人  SmileFans

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


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

上一篇下一篇

猜你喜欢

热点阅读