简易画板

UIImage的一个类别,用来保存图片

H文件

#import <UIKit/UIKit.h>

@interface UIImage (JJCaptureView)

+ (UIImage *)JJCaptureView:(UIView *)view;

@end


m文件

#import "UIImage+JJCaptureView.h"

@implementation UIImage (JJCaptureView)

+ (UIImage *)JJCaptureView:(UIView *)view

{

    // 创建图形上下文

    UIGraphicsBeginImageContext(view.frame.size);

    // viewlayer绘制到该上下文中

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    // 取出绘制好的图片

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    return newImage;

}

@end


自定义画板H文件

#import <UIKit/UIKit.h>


@interface JJDrawView : UIView

- (void)cancel;

- (void)clear;

@end



自定义画板m文件

自定义画板H文件

#import "JJDrawView.h"


@interface JJDrawView() {

    NSMutableArray *_paths;

}

@end


@implementation JJDrawView


- (void)dealloc {

    [_paths release];

    [super dealloc];

}

- (instancetype)init

{

    self = [super init];

    if (self) {

        _paths = [[NSMutableArray alloc] initWithCapacity:0];

    }

    return self;

}

- (NSMutableArray *)paths {

    if (_paths == nil) {

        _paths = [NSMutableArray array];

    }

    return _paths;

}

- (void)cancel {

    [_paths removeLastObject];

    [self setNeedsDisplay];

}

- (void)clear {

    [_paths removeAllObjects];

    [self setNeedsDisplay];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

    CGPoint beganPoint = [touch locationInView:touch.view];

    // 创建路径

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path setLineJoinStyle:kCGLineJoinRound];

    [path setLineCapStyle:kCGLineCapRound];

    [path moveToPoint:beganPoint];

    [_paths addObject:path];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

    CGPoint movePoint = [touch locationInView:touch.view];

    UIBezierPath *movePath = [_paths lastObject];

    [movePath addLineToPoint:movePoint];

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

    for (UIBezierPath *path in _paths) {

        [path stroke];

    }

}

@end

使用

自定义画板H文件

#import "ViewController.h"

#import "JJDrawView.h"

#import "UIImage+JJCaptureView.h"


@interface ViewController ()

{

    JJDrawView *drawView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor lightGrayColor];

    drawView = [[JJDrawView alloc] init];

    drawView.frame = CGRectMake(0.0, 20.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);

    drawView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:drawView];

    [drawView release];

    

    NSArray *titles = @[@"撤销", @"清空", @"保存"];

    // 放至三个按钮

    for (int i = 0; i < 3; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(10.0+(i*100), drawView.frame.origin.y+drawView.frame.size.height+20.0, 80.0, 40.0);

        btn.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];

        [btn setTitle:titles[i] forState:UIControlStateNormal];

        btn.backgroundColor = [UIColor blueColor];

        [self.view addSubview:btn];

        btn.tag = 111+i;

        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    }

}

- (void)click:(UIButton *)btn

{

    if (btn.tag == 111) {

        [drawView cancel];

    } else if (btn.tag == 112) {

        [drawView clear];

    } else {

        UIImage *newImage = [UIImage JJCaptureView:drawView];

        UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    if (error) {

        NSLog(@"保存失败,请检查权限");

    } else {

        NSLog(@"成功");

    }

}

@end



自定义画板H文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值