二维码相机遮罩层快速实现

一、应用场景

在开发相机相关应用,如图片裁剪,二维码时,我们经常需要在最上层覆盖一层遮罩,如下图:


一个通常的做法(以上图为例),是将界面划分成几个部分,分别绘制,如阴影部分,分上下左右四个部分,然后分别绘制这四个部分,再绘制绿色边框。实现也不难,但太过于繁琐,现在给出一个更简单的方法

#import <UIKit/UIKit.h>

@interface ILMaskView : UIView

@property (unsafe_unretained, nonatomic)CGRect cropRect;
@property (strong, nonatomic) UIColor *borderColor;

@end


#import "ILMaskView.h"

#define kMaskViewBorderWidth 2.0f

@interface ILMaskView ()
@end

@implementation ILMaskView


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.6);
    CGContextFillRect(ctx, self.bounds);
    
    CGContextSetStrokeColorWithColor(ctx, self.borderColor.CGColor);
    CGContextStrokeRectWithWidth(ctx, _cropRect, kMaskViewBorderWidth);
    
    CGContextClearRect(ctx, _cropRect);
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值