2016-05-09 20 views
5

tôi đã sử dụng mã này, nó hoạt động một phần nhưng hình ảnh không cắt đúng output seen like thisCGContextClearRect không hoạt động mượt mà trong trường hợp touchesMoved trong c khách quan

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:_img.superview]; 

UIGraphicsBeginImageContext(self.img.frame.size); 
[_img.image drawInRect:CGRectMake(0, 0, _img.frame.size.width, _img.frame.size.height)]; 

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 

CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 15, 15)); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
_img.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
} 

Trả lời

5

tôi phải đối mặt với cùng một vấn đề trước sau đó tôi đã cố gắng bên dưới mã và nó làm việc cho tôi

UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.img_main]; 
// NSLog(@"Current ponint : %f,%f",currentPoint.x,currentPoint.y); 

self.img_main.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
self.img_main.contentMode = UIViewContentModeScaleAspectFit; 

UIGraphicsBeginImageContextWithOptions(self.img_main.bounds.size, NO, 0.0); 
[_img_main.image drawInRect:CGRectMake(0,0, self.img_main.bounds.size.width,self.img_main.bounds.size.height)]; 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

//For clearing Size and Mode 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush_Size); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDestinationOut); 
//For Clearing Opacity   
CGContextSetAlpha(UIGraphicsGetCurrentContext(), brush_opacity); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 

_img_main.image = UIGraphicsGetImageFromCurrentImageContext(); 

CGContextFlush(UIGraphicsGetCurrentContext()); 
UIGraphicsEndImageContext(); 
+0

thanks viraj Padsala it works for me !! –

Các vấn đề liên quan