畫板例子實現

畫板例子思路

步驟一:自定義一個畫板視圖
步驟二:監聽手指觸摸事件,當手指在視圖滑動的時候,就畫線.
2.1在手指開始觸摸的時候,創建可變路徑,並且設置路徑起始點。
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:
self];
    SUNBezierPath *path = [SUNBezierPath bezierPathWithWidth:_width andWithColor:_color andWithPoint:point];
    _pathU = path;
    [
self.arrayM addObject:path];
   
}

2.2. 當手指移動的時候,給路徑添加目標點,並且重新繪製視圖。
- (
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:
self];
    [_pathU addLineToPoint:point];
   
// 重繪
   
 [self setNeedsDisplay];
}

2.3 當手指觸摸結束,將路徑保存起來,並釋放當前路徑
- (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_pathM addObject:_pathU];
    CGPathRelease(_pathU.CGPath);
    NSLog(
@"%s",__func__);
}
注意:
1. [self setNeedsDisplay];這段代碼不能少,否則在畫板畫不出東西。
2.當畫板與slider綁定時,要通過awakeFromNib設置線寬,不然線寬默認爲0.
- (void)awakeFromNib
{
   
_width = 2;
}
步驟三:實現繪製視圖方法
3.1. 自定義路徑類
+ (
instancetype)bezierPathWithWidth:(CGFloat)width andWithColor:(UIColor *)color andWithPoint:(CGPoint)point
{
    SUNBezierPath *path = [[SUNBezierPath alloc] init];
    path.lineWidth = width;
    path.color = color;
    [path moveToPoint:point];
   
return path;
}

3.2.實現繪圖方法
- (
void)drawRect:(CGRect)rect
{
   
for (SUNBezierPath *path in self.arrayM) {
       
if ([path isKindOfClass:[UIImage class]])  {
            UIImage *image = (UIImage *)path;
            [image drawAtPoint:CGPointZero];
        }
else{
            [path.color set];
            [path stroke];
        }
    }

步驟四、在storyboard中定義工具欄
4.1 清屏
- (void)clearScreen
{
    [
self.arrayM removeAllObjects];
    [
self setNeedsDisplay];
}
直接把數組removeAllObjects
4.2 撤銷
- (void)undo
{
    [
self.arrayM removeLastObject];
    [
self setNeedsDisplay];
}

4.3 保存
- (IBAction)save:(id)sender
{
   
// 截屏
   
// 開啓上下文
   
UIGraphicsBeginImageContextWithOptions(_drawingView.bounds.size, NO, 0.0);
   
   
// 獲得當前上下文
   
CGContextRef ctr = UIGraphicsGetCurrentContext();
   
// 把畫板上的內容渲染都上下文
    [
_drawingView.layer renderInContext:ctr];
   
   
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   
   
// 關閉上下文
   
UIGraphicsEndImageContext();
   
   
// 保存到相冊裏面
   
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
調用MBProgressHUD 第三方框架
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
   
if (error) {// 保存失敗
        [
MBProgressHUD showError:@"保存失敗"];
    }
else{// 保存成功
        [
MBProgressHUD showSuccess:@"保存成功"];
    }
}

4.4 照片
- (IBAction)selectedPicture:(id)sender
{
   
// 去用戶的操作
   
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   
    picker.
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [
self presentViewController:picker animated:YES completion:nil];
    picker.
delegate = self;

}

- (
void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   
NSLog(@"%@",info);
   
UIImage *newImage = info[UIImagePickerControllerOriginalImage];
   
SUNHandleView *handleV = [[SUNHandleView alloc] initWithFrame:self.drawingView.frame];
    handleV.
delegate = self;
    handleV.
image = newImage;
    [
self.view addSubview:handleV];
    [
self dismissViewControllerAnimated:YES completion:nil];

}
注意:去模擬器用戶的相冊要遵守UIImagePickerControllerDelegate協議,實現
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
以modal的形式
[self presentViewController:picker animated:YES completion:nil];
關閉當初Modal出來的控制器
[self dismissViewControllerAnimated:YES completion:nil];

步驟五、實現從相處裏面選擇一張圖片能夠用手勢操作。

添加手勢
- (
void)addGestureRecognizer
{
   
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    pan.
delegate = self;
    [
_imageView addGestureRecognizer:pan];
   
   
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.
delegate = self;
    [
_imageView addGestureRecognizer:rotation];
   
   
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.
delegate = self;
    [
_imageView addGestureRecognizer:pinch];
   
   
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.
delegate = self;
    [
_imageView addGestureRecognizer:longPress];
}

- (
void)pan:(UIPanGestureRecognizer *)pan
{
   
CGPoint point = [pan translationInView:self];
   
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, point.x, point.y);
    [pan
setTranslation:CGPointZero inView:_imageView];
}

- (
void)rotation:(UIRotationGestureRecognizer *)rotation
{
   
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);
    rotation.
rotation = 0;
}

- (
void)pinch:(UIPinchGestureRecognizer *)pinch
{
   
_imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
    pinch.
scale = 1;
}

實現長按的操作
- (
void)longPress:(UILongPressGestureRecognizer *)longPress
{
   
if (longPress.state == UIGestureRecognizerStateEnded) {
        [
UIView animateWithDuration:0.5 animations:^{
           
_imageView.alpha = 0.3;
        }
completion:^(BOOL finished) {
            [
UIView animateWithDuration:0.5 animations:^{
               
_imageView.alpha = 1;
            }
completion:^(BOOL finished) {
               
// 截屏
               
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
               
CGContextRef ctr = UIGraphicsGetCurrentContext();
                [
self.layer renderInContext:ctr];
               
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
               
NSData *data = UIImagePNGRepresentation(image);
                [data
writeToFile:@"/Users/sunjinshuai/Desktop/image.png" atomically:YES];
               
UIGraphicsEndImageContext();
               
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
               
// 把圖片傳給控制器
               
if ([self.delegate respondsToSelector:@selector(handleView:withImage:)]) {
                    [
self.delegate handleView:self withImage:image];
                }
               
// 從父控件中移除
                [
self removeFromSuperview];
            }];
        }];
       
    }
}

- (
void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
   
if (error) {
        [
MBProgressHUD showError:@"保存失敗"];
    }
else{
        [
MBProgressHUD showSuccess:@"保存成功"];
    }
}

- (
BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
   
return YES;
}

- (
void)setImage:(UIImage *)image
{
   
_image = image;
   
_imageView.image = image;
}

- (
void)addImageView
{
   
UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.bounds];
    imageV.
userInteractionEnabled = YES;
   
_imageView = imageV;
    [
self addSubview:imageV];
}
發佈了82 篇原創文章 · 獲贊 13 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章