2014-10-28 60 views
0

我正在嘗試在我的UIImage(在UIImageView的內部)使用觸摸事件添加繪圖功能。但是,對於某些圖像,當我開始繪製圖像時,圖像開始收縮並變得越來越模糊,而我越來越模糊。我主要使用ray wenderlich的舊繪圖教程作爲參考,只是更改了值以匹配我的UI。這兩個圖像顯示你當我畫會發生什麼:在uiimageview上繪圖是調整uiimage的大小?

Before drawing

After

它看起來像我使用繪圖漂亮的標準方法。我的直覺是界限/矩形繪圖方法導致某種問題。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(_allowDoodling == NO) return; 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch previousLocationInView:self.imageView]; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(_allowDoodling == NO) return; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.imageView]; 

    UIGraphicsBeginImageContextWithOptions(_imageView.frame.size, NO, 2.0f); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    [_imageView.image drawInRect:_imageView.bounds]; 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); 
    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, 5.0); 
    CGContextSetStrokeColorWithColor(context, _doodleColor.CGColor); 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    _imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(_allowDoodling == NO) return; 

    UIGraphicsBeginImageContextWithOptions(_imageView.frame.size, NO, 2.0f); 
    [_imageView.image drawInRect:_imageView.bounds]; 
    _imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    UIGraphicsEndImageContext(); 
} 

回答

0

它更多鈔票,你必須使用

UIGraphicsBeginImageContextWithOptions(CGSizeMake(_imageView.frame.size.width * 2, _imageView.frame.size.width * 2), NO, 1.0f);