OC가 쓴 화판의 데모.
이것은 화판의 항목 부분 코드이다
잠금 해제 후 UINavgationController 추가
FunctionViewController *vc = [[FunctionViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[nav setNavigationBarHidden:YES];
[self presentViewController:nav animated:YES completion:nil];
도판을 그리면 손가락 조작이 화면을 떠난 후에도 계속 그림을 그릴 수 있다
// ,lineModel , CGMutablePathRef
@property(nonatomic, strong) NSMutableArray *lineModels;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
LineModel *lineModel = [[LineModel alloc] init];
lineModel.path = CGPathCreateMutableCopy(self.path); // self.path
lineModel.lineWidth = 2;
lineModel.color = [UIColor blackColor];
[self.lineModels addObject:lineModel];
CGPathRelease(self.path);
self.path = nil;
}
- (void)drawRect:(CGRect)rect {
//
[self.lineModels enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
LineModel *model = obj;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, model.path);
CGContextSetLineWidth(context, model.lineWidth);
CGContextSetStrokeColorWithColor(context, model.color.CGColor);
CGContextDrawPath(context, kCGPathStroke);
}];
if (self.path) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, self.path);
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextDrawPath(context, kCGPathStroke);
}
}
다양한 작업을 위한 새 ToolView 클래스
#import
@interface ToolView : UIView
@property (nonatomic, copy) void(^penBlock)(void);
@property (nonatomic, copy) void(^eraserBlock)(void);
@property (nonatomic, copy) void(^colorBlock)(void);
@property (nonatomic, copy) void(^undoBlock)(void);
@property (nonatomic, copy) void(^clearBlock)(void);
@property (nonatomic, copy) void(^saveBlock)(void);
@property (nonatomic, copy) void(^sliderBlock)(CGFloat width);
@end
-(void) configToolView {
__weak typeof (self) weakself = self;
self.toolView.penBlock = ^{
weakself.bEraserMode = NO;
weakself.canvasView.color = [UIColor blackColor]; //weakself.lastColor;
weakself.canvasView.lineWidth = weakself.lastLineWidth;
};
self.toolView.eraserBlock = ^{
weakself.bEraserMode = YES;
weakself.canvasView.color = [UIColor whiteColor];
weakself.canvasView.lineWidth = 50;
};
self.toolView.colorBlock = ^{
};
self.toolView.undoBlock = ^{
[weakself.canvasView undo];
};
self.toolView.clearBlock = ^{
[weakself.canvasView clear];
};
self.toolView.saveBlock = ^{
};
self.toolView.sliderBlock = ^(CGFloat width) {
if (!weakself.bEraserMode) {
weakself.canvasView.lineWidth = width;
}
weakself.lastLineWidth = width;
};
}
@property BOOL bEraserMode;
@property UIColor *lastColor;
@property CGFloat lastLineWidth;
#import
@interface CanvasView : UIView
...
-(void)undo;
-(void)clear;
...
@end
도판을 그리다
-(ColorView *) colorView {
if (!_colorView) {
_colorView = [[ColorView alloc] initWithFrame:self.view.frame];
}
return _colorView;
}
@interface ColorView : UIView
...
@property (nonatomic, copy) void(^selectColorBlock)(UIColor *color);
...
@end
-(ColorView *) colorView {
if (!_colorView) {
_colorView = [[ColorView alloc] initWithFrame:self.view.frame];
__weak typeof (self) weakify = self;
_colorView.selectColorBlock = ^(UIColor *color) {
if (!weakify.bEraserMode) {
weakify.canvasView.color = color;
}
weakify.lastColor = color;
};
}
return _colorView;
}
앨범에 저장
// info.pkist
NSPhotoLibraryUsageDescription
NSPhotoLibraryAddUsageDescription
//
#import
#import
...
// block
self.toolView.saveBlock = ^{
[weakself saveImageToAlbum:[weakself screenView:weakself.canvasView]];
};
...
//
-(UIImage *) screenView:(UIView *)view {
CGRect rect = view.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
...
/**
@param image image
*/
-(void) saveImageToAlbum:(UIImage *)image {
//
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
NSLog(@" ");
} else {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),(__bridge void *)self);
}
}
원본 코드