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);
// 将view的layer绘制到该上下文中
[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