![]()
//
// HMPaintView.h
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HMPaintView : UIView
@property(nonatomic,assign)CGFloat width;
@property(nonatomic,strong)UIColor *color;
@property(nonatomic,strong)UIImage *image;
- (void)clearScreen;
- (void)undo;
@end
//
// HMPaintView.m
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import "HMPaintView.h"
#import "HMPaintPath.h"
@interface HMPaintView()
@property(nonatomic,strong)UIBezierPath *path;
@property(nonatomic,strong)NSMutableArray *paths;
@end
@implementation HMPaintView
- (NSMutableArray *)paths
{
if (_paths == nil) {
_paths = [NSMutableArray array];
}
return _paths;
}
- (CGPoint)pointWithTouches:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
return [touch locationInView:self];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pos = [self pointWithTouches:touches];
HMPaintPath *path = [HMPaintPath paintPathWithLineWidth:_width color:_color startPoint:pos];
_path =path;
[self.paths addObject:path];
}
-(void)awakeFromNib
{
_width = 2;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pos = [self pointWithTouches:touches];
[_path addLineToPoint:pos];
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
if (!self.paths.count) return;
for (HMPaintPath *path in self.paths) {
if ([path isKindOfClass:[UIImage class]]) {
UIImage *image = (UIImage *)path;
[image drawAtPoint:CGPointZero];
}else
{
[path.color set];
[path stroke];
}
}
}
- (void)clearScreen
{
[self.paths removeAllObjects];
[self setNeedsDisplay];
}
-(void)undo
{
[self.paths removeLastObject];
[self setNeedsDisplay];
}
- (void)setIamge:(UIImage *)image
{
_image = image;
[self.paths addObject:image];
[self setNeedsDisplay];
}
@end
//
// HMPaintPath.h
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HMPaintPath : UIBezierPath
@property(nonatomic,strong)UIColor *color;
+ (instancetype)paintPathWithLineWidth:(CGFloat)width color:(UIColor *)color startPoint:(CGPoint)startP;
@end
//
// HMPaintPath.m
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import "HMPaintPath.h"
@implementation HMPaintPath
+ (instancetype)paintPathWithLineWidth:(CGFloat)width color:(UIColor *)color startPoint:(CGPoint)startP
{
HMPaintPath *path = [[self alloc] init];
path.lineWidth = width;
path.color = color;
[path moveToPoint:startP];
return path;
}
@end
//
// HMHandleImageView.h
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HMHandleImageView : UIView
@end
------------手势操作------------------
//
// HMHandleImageView.m
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import "HMHandleImageView.h"
@implementation HMHandleImageView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
//
// ViewController.h
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// 画板
//
// Created by YaguangZhu on 15/9/10.
// Copyright (c) 2015年 YaguangZhu. All rights reserved.
//
#import "ViewController.h"
#import "HMPaintView.h"
#import "MBProgressHUD+MJ.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet HMPaintView *paintView;
@end
@implementation ViewController
- (IBAction)save:(id)sender {
UIGraphicsBeginImageContextWithOptions(_paintView.bounds.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[_paintView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[MBProgressHUD showError:@"保存失败"];
}else
{
[MBProgressHUD showSuccess:@"保存成功"];
}
}
- (IBAction)selectPicture:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
_paintView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)eraser:(id)sender {
_paintView.color = [UIColor lightGrayColor];
}
- (IBAction)undo:(id)sender {
[_paintView undo];
}
- (IBAction)clearScreen:(id)sender {
[_paintView clearScreen];
}
- (IBAction)colorClick:(UIButton *)sender {
_paintView.color = sender.backgroundColor;
}
- (IBAction)valueChange:(UISlider *)sender {
_paintView.width = sender.value;
}
- (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.
}
@end