iOS简单画板开发案例分享

2020-01-15 13:58:57于丽

从相册选择完图片后把图片显示在画板上了但是还没有渲染到layer,这时候需要对图片进行移动缩放旋转这些操作的话,但是UIImage是不能拉伸旋转这些操作的,UIImageView才可以,所以解决思路就是自定义一个view来专门处理对图片的操作,在自定义view上放一个UIImageView,从相册选择图片后获取的image设置给UIImageView,这样的自定义view上操作UIIamgeView。


#import <UIKit/UIKit.h>
 
@interface ImageHandleView : UIView
/** 图片 */
@property(nonatomic, strong) UIImage *image;
 
/** block */
@property(nonatomic, strong) void(^handleCompletionBlock)(UIImage *image);
@end

#import "ImageHandleView.h"
 
@interface ImageHandleView () <UIGestureRecognizerDelegate>
 
/** image */
@property(nonatomic, weak) UIImageView *imageView;
 
@end
 
@implementation ImageHandleView
 
//防止图片上的触摸事件传递到画板
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
   
  return _imageView;
}
 
- (UIImageView *)imageView {
   
  if (_imageView == nil) {
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:self.bounds];
     
    _imageView = imageV;
     
    //设置imgaeview允许与用户交互
    _imageView.userInteractionEnabled = YES;
     
    //添加手势
    [self setUpGestureRecognizer];
     
    //把这个imageview添加到图片处理的view上
    [self addSubview:imageV];
     
  }
  return _imageView;
}
 
#pragma mark - 添加手势
- (void)setUpGestureRecognizer {
   
  //平移手势
  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
  [_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:)];
  [_imageView addGestureRecognizer:longPress];
   
}
 
#pragma mark - 处理平移手势
- (void)pan:(UIPanGestureRecognizer *)pan {
   
  //获取手指的偏移量
  CGPoint tranp = [pan translationInView:self.imageView];
   
  //设置imageview的形变
  self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, tranp.x, tranp.y);
   
  //复位
  [pan setTranslation:CGPointZero inView:self.imageView];
   
}
 
#pragma mark - 处理旋转手势
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
   
  //设置imageview的形变
  self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
   
  //复位
  rotation.rotation = 0;
   
}
 
#pragma mark - 处理缩放手势
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
   
  //设置imageview的形变
  self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
   
  //复位
  pinch.scale = 1;
   
}
 
#pragma mark - 处理长按手势
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
   
  //图片处理完成
  if (longPress.state == UIGestureRecognizerStateBegan) {
     
    //高亮效果
    [UIView animateWithDuration:0.25 animations:^{
      self.imageView.alpha = 0;
    } completion:^(BOOL finished) {
      
      [UIView animateWithDuration:0.25 animations:^{
        self.imageView.alpha = 1;
      } completion:^(BOOL finished) {
         
        //高亮时生成一张新的图片
        //开启位图上下文
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
         
        //获取位图上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
         
        //把控件的图层渲染到上下文
        [self.layer renderInContext:ctx];
         
        //从上下文中获取新的图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
         
        //关闭上下文
        UIGraphicsEndImageContext();
         
        //调用block
        if(_handleCompletionBlock) {
          _handleCompletionBlock(image);
        }
         
        //移除父控件
        [self removeFromSuperview];
         
      }];
       
    }];
     
     
  }
   
}
 
#pragma mark - 手势代理方法 <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
   
  //yes表示同时支持多个手势
  return YES;
   
}
 
- (void)setImage:(UIImage *)image {
   
  _image = image;
   
  //把图片展示到UIImageView上
  self.imageView.image = image;
 
}
 
@end