码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发_图片涂鸦

时间:2016-01-23 22:55:34      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

主控制器的关键代码:

一  ViewController.m

#import "ViewController.h"
#import "ZRPaintView.h"
#import "UIImage+ZR.h"

@interface ViewController ()

- (IBAction)clear;
- (IBAction)back;
- (IBAction)save;

@property (weak, nonatomic) IBOutlet ZRPaintView *paintView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clear {
    [self.paintView clear];
}

- (IBAction)back {
    [self.paintView back];
}

- (IBAction)save {
    //1 截图
    UIImage *image = [UIImage captureWithView:self.paintView];
    //2 保存到图片
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}
/**
 *  保存图片操作之后就会调用
 *
 *  @param image       image
 *  @param error       error
 */
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        NSLog(@"保存失败");
    }else{
        NSLog(@"保存成功");
    }
}
@end

 

 

二  创建一个继承自UIView的子类:

ZRPaintView.h

#import <UIKit/UIKit.h>

@interface ZRPaintView : UIView

-(void)clear;

-(void)back;

@end

 

ZRPaintView.m

#import "ZRPaintView.h"


@interface ZRPaintView ()
@property(nonatomic,strong) NSMutableArray *totalPathPoints;

//用来记录
@property(nonatomic,strong) NSMutableArray *pathPoints;
@end

@implementation ZRPaintView

-(NSMutableArray *)totalPathPoints{
    if (!_pathPoints) {
        _pathPoints = [NSMutableArray array];
    }
    return _pathPoints;
}

-(void)clear{
    [self.totalPathPoints removeAllObjects];
    [self setNeedsDisplay];
}

-(void)back{
    [self.totalPathPoints removeLastObject];
    [self setNeedsDisplay];
}


/**
 *  确定起点
 */
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint startPos = [touch locationInView:touch.view];
    //每一次开始触摸,就新建一个数组来存放这一次触摸过程中所有点(这次触摸过程中得所有点)
    NSMutableArray *pathPoints = [NSMutableArray array];
    [pathPoints addObject:[NSValue valueWithCGPoint:startPos]];
    [self.totalPathPoints addObject:pathPoints];
    //添加这个路径中得所有点到数组中
    [self setNeedsDisplay];
}
/**
 *  连线
 */
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPos = [touch locationInView:touch.view];
    //取出这次路径对应的数组
    NSMutableArray *pathPoints = [self.totalPathPoints lastObject];
    [pathPoints addObject:[NSValue valueWithCGPoint:currentPos]];
    
    [self setNeedsDisplay];
}
/**
 *  连线
 */
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//    CGPoint endPos = [touch locationInView:touch.view];
//    
//    //取出这次路径对应的数组
//    NSMutableArray *pathPoints = [self.totalPathPoints lastObject];
//    [pathPoints addObject:[NSValue valueWithCGPoint:endPos]];
//    
//    [self setNeedsDisplay];
    [self touchesMoved:touches withEvent:event];
}


- (void)drawRect:(CGRect)rect {
//    UIRectFill(CGRectMake(0, 0, 100, 100));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    for (NSMutableArray *pathPoints in self.totalPathPoints) {
        for (int i = 0; i<pathPoints.count; i++) {//一条路径
            CGPoint pos = [pathPoints[i] CGPointValue];
            if (i==0) {
                CGContextMoveToPoint(ctx, pos.x, pos.y);
            }else{
                CGContextAddLineToPoint(ctx, pos.x, pos.y);
            }
        }
    }
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextSetLineWidth(ctx, 5);
    CGContextStrokePath(ctx);
}

@end

 

三  创建UIImage分类文件:

UIImage+ZR.h

#import <UIKit/UIKit.h>

@interface UIImage (ZR)

+(instancetype)captureWithView:(UIView *)view;

@end

 

UIImage+ZR.m

#import "UIImage+ZR.h"

@implementation UIImage (ZR)

+(instancetype)captureWithView:(UIView *)view{
    //1 开启上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    //2 将控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    //3 取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4 结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end

 

iOS开发_图片涂鸦

标签:

原文地址:http://www.cnblogs.com/xuanzhangran123/p/5154121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!