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

UIAlertView是消息提示框UI控件藐窄,對于消息提示框的中的按鈕事件采用的是事件委托機制。要實現(xiàn)事件響應酬土,需要實現(xiàn)對應協(xié)議荆忍、重寫函數(shù)達到目的。

本文介紹
1撤缴、UIAlertView+Block刹枉,簡化代碼
2、自定義Alert+Block 簡單的自定義彈框封裝

正常寫法

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 
                                                  message:@"這是一個警告框屈呕!" 
                                                  delegate:self  
                                                  cancelButtonTitle:@"確定" 
                                                  otherButtonTitles:nil];  
 [alert show]; 



//根據(jù)被點擊按鈕的索引處理點擊事件
-(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:@"分層是表示將功能進行有序的分組:應用程序專用功能位于上層种樱,跨越應用程序領域的功能位于中層,而配置環(huán)境專用功能位于低層俊卤。分層從邏輯上將子系統(tǒng)劃分成許多集合嫩挤,而層間關系的形成要遵循一定的規(guī)則。"
                              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


我是寫代碼的凡消恍,如有錯誤岂昭,歡迎指正!??????

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末狠怨,一起剝皮案震驚了整個濱河市约啊,隨后出現(xiàn)的幾起案子邑遏,更是在濱河造成了極大的恐慌,老刑警劉巖恰矩,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件记盒,死亡現(xiàn)場離奇詭異,居然都是意外死亡外傅,警方通過查閱死者的電腦和手機纪吮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萎胰,“玉大人碾盟,你說我怎么就攤上這事〖季梗” “怎么了冰肴?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵,是天一觀的道長榔组。 經(jīng)常有香客問我熙尉,道長,這世上最難降的妖魔是什么瓷患? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任骡尽,我火速辦了婚禮,結果婚禮上擅编,老公的妹妹穿的比我還像新娘攀细。我一直安慰自己,他們只是感情好爱态,可當我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布谭贪。 她就那樣靜靜地躺著,像睡著了一般锦担。 火紅的嫁衣襯著肌膚如雪俭识。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天洞渔,我揣著相機與錄音套媚,去河邊找鬼。 笑死磁椒,一個胖子當著我的面吹牛堤瘤,可吹牛的內容都是我干的。 我是一名探鬼主播浆熔,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼本辐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起慎皱,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤老虫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茫多,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體祈匙,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年地梨,在試婚紗的時候發(fā)現(xiàn)自己被綠了菊卷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡宝剖,死狀恐怖洁闰,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情万细,我是刑警寧澤扑眉,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站赖钞,受9級特大地震影響腰素,放射性物質發(fā)生泄漏。R本人自食惡果不足惜雪营,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一弓千、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧献起,春花似錦洋访、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至岂嗓,卻和暖如春汁展,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背厌殉。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工食绿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人公罕。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓器紧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熏兄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件摩桶、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,129評論 4 61
  • 查閱 DAY1 - DAY6 的同學優(yōu)秀作業(yè)(助教稍后會整理提供)桥状,寫出自己具體學到了哪些以及哪些可以改進的地方。...
    王小錘子閱讀 371評論 0 0
  • 今天周六硝清,舍友去別的礦找同學了辅斟,我想自己安靜休息會兒就婉拒了舍友的邀請。 真當我一個人時芦拿,內心并不安寧士飒,小人閑居為...
    生命的朝拜者閱讀 170評論 0 0
  • 我知道我有點任性 我知道我有點調皮 我知道我有一點點的傻 但是我也知道 不論我是什么樣子 你們都會是最愛我的 我最...
    云清一閱讀 209評論 3 6
  • 陽光冷 清秋月 古樹長藤 蛛網(wǎng)蔽闕 一夕長空云朦朦 半宿飛葉風孑孓 長劍寒 飛花亂 別姬霸王 雪殘橋斷 三生酒醒夢...
    茗香酒影閱讀 163評論 2 3