我有一张UIImageView
显示图片。
我注册了一个缩放手势来放大/缩小和一个平移手势来绘制它(我有充分的理由使用平移手势,而不是 touchMoved)
这是捏目标:
- (void)pinchGestureActionOnSubject:(UIPinchGestureRecognizer *)pinch
{
if([(UIPinchGestureRecognizer*)pinch state] == UIGestureRecognizerStateBegan)
{
_lastScale = 1.0;
}
CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)pinch scale]);
CGAffineTransform currentTransform = self.imageViewSubject.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self.imageViewSubject setTransform:newTransform];
_lastScale = [(UIPinchGestureRecognizer*)pinch scale];
}
这是泛目标:
-(void) panOnSubjectForDrawing:(id)sender
{
if([sender numberOfTouches] > 0)
{
CGPoint currentPoint = [sender locationOfTouch:0 inView:self.imageViewSubject];
if(lastPoint.x == 0 || lastPoint.y == 0)
{
lastPoint.x = currentPoint.x;
lastPoint.y = currentPoint.y;
}
UIGraphicsBeginImageContext(self.imageViewSubject.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.imageViewSubject.image drawInRect:CGRectMake(0, 0, self.imageViewSubject.bounds.size.width, self.imageViewSubject.bounds.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 10); // Epaisseur du trait
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); // Transparent
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
self.imageViewSubject.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
}
问题是如果我放大后绘制,图片会变得模糊。但如果我在没有缩放的情况下绘制,图像会保持不变。
我听说这可能是因为我的框架必须是整数,所以我添加:
self.imageViewSubjectframe = CGRectIntegral(self.imageViewSubject.frame);
在我画之前,但这是最糟糕的,更模糊。
任何想法?