图像擦除和绘图捏手势奇怪的问题

时间:2017-05-15 12:32:15

标签: uiimageview uiimage erase uigraphicscontext uipinchgesturerecognizer

这里我以编程方式创建UIImageview并根据UIImage给出其框架。我使用UIPinchGestureRecogniser来缩放图像。这是我的class

以下是图片的网址:http://52.43.193.229/emoji_docs/14944216111494421608_93148.jpg

这是我的代码

//

//  ImageEditViewController.m
//  LogoEmojis
//
//  Created by Benzatine Infotech on 5/15/17.
//  Copyright © 2017 Benzatine Infotech. All rights reserved.
//

#import "ImageEditViewController.h"

@interface ImageEditViewController ()


@property (nonatomic, weak) LCPaintView *paintView;
@property(strong,nonatomic) UIImageView *imgview;

@end

@implementation ImageEditViewController

#pragma -mark All view Methods
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.lbl_edit.font = MEDIUM_16;
    self.lbl_edit.textColor = LABEL_BLACK_COLOR;

    UIImageView *img = [[UIImageView alloc]init];
    __weak UIImageView *weakImage = img ;

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url_img];

    [img setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"ic_home_placeholder_01"] success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
        weakImage.image = image;
    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

    _imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, weakImage.image.size.width, weakImage.image.size.height)];
    _imgview.transform = CGAffineTransformMakeScale(1, 1);
    _imgview.center = CGPointMake(self.view_imgview.frame.size.width  / 2,
                                  self.view_imgview.frame.size.height / 2);
    _imgview.image = weakImage.image;
    [self.view_imgview addSubview:_imgview];
    self.view_imgview.userInteractionEnabled = YES;
    self.imgview.userInteractionEnabled = YES;

    self.btn_reset.hidden = YES;
    [self addPinchGesture];
    //[self addPanGesture];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

#pragma -mark PinchGesture method
-(void)addPinchGesture{
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchWithGestureRecognizer:)];
    pinchGestureRecognizer.delegate = self;
    [self.imgview addGestureRecognizer:pinchGestureRecognizer];
}
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
    self.imgview.transform = CGAffineTransformScale(self.imgview.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
    pinchGestureRecognizer.scale = 1.0;
}

#pragma -mark Pan gesture method
-(void)addPanGesture{
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveViewWithGestureRecognizer:)];
    panGestureRecognizer.delegate = self;
    [self.imgview addGestureRecognizer:panGestureRecognizer];
}
-(void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer{
    CGPoint touchLocation = [panGestureRecognizer locationInView:self.view];
    self.imgview.center = touchLocation;
}
#pragma -mark All Action methods
- (IBAction)btn_click_save:(id)sender {
    NSLog(@"%f , %f",_imgview.frame.size.width, _imgview.frame.size.height);
    addImageToCameraRoll(_imgview.image, @"LogoMojis", self);
}

- (IBAction)btn_Click_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btn_click_eraser:(id)sender {
    if(self.btn_eraser.selected == NO){
        self.btn_eraser.selected = YES;
        self.imgview.userInteractionEnabled = NO;

    }else{
        self.imgview.userInteractionEnabled = YES;
        self.btn_eraser.selected = NO;
    }
}



- (IBAction)btn_click_draw:(id)sender {
    LCPaintView *paintView = [[LCPaintView alloc] init];
    paintView.lineWidth = 5.0;
    paintView.frame = CGRectMake(0, 0, _imgview.frame.size.width, _imgview.frame.size.height);
    paintView.lineColor = [UIColor blackColor];
    [_imgview addSubview:paintView];
    self.paintView = paintView;
    paintView.userInteractionEnabled = YES;
    [_imgview bringSubviewToFront:self.paintView];
}

- (IBAction)value_changed_slider:(id)sender {

}
#pragma -mark Touches method Eraser
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.btn_eraser.selected == YES) {
        UITouch *touch = [touches anyObject];
        lastPoint = [touch locationInView:self.imgview];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.btn_eraser.selected == YES) {
        UITouch *touch = [touches anyObject];
        currentTouch = [touch locationInView:_imgview];
        self.btn_reset.hidden = NO;
        BOOL isPointInsideView = [_imgview pointInside:currentTouch withEvent:nil];

        if(isPointInsideView){
            UIGraphicsBeginImageContext(_imgview.frame.size);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(_imgview.image.size.width, _imgview.image.size.height), NO, 1.0);
            [_imgview.image drawInRect:CGRectMake(0, 0, _imgview.image.size.width, _imgview.image.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
            CGContextBeginPath(UIGraphicsGetCurrentContext());
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDestinationOut);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentTouch.x, currentTouch.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            _imgview.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            lastPoint = currentTouch;
        }
    }
}
@end

现在问题就像当我尝试擦除或删除图像时,它会像VIDEO一样留下触摸或拖动。突然触摸事件离开imageview。我不明白为什么事件会离开imageview。

我哪里出错?任何帮助都会受到赞赏。 请帮帮我 谢谢。

0 个答案:

没有答案