簡體   English   中英

在uiimageview上繪圖會調整uiimage的大小嗎?

[英]Drawing on uiimageview is resizes uiimage?

我試圖在UIImage (在UIImageView )使用觸摸事件添加繪圖功能。 但是,對於某些圖像,當我開始繪制圖像時,我在其上繪制的圖像越多,圖像就會開始縮小並變得模糊。 我主要使用ray wenderlich的舊繪圖教程作為參考,只是更改了值以匹配我的UI。 這兩個圖像向您展示了我繪制時發生的情況:

繪圖前

后

看來我在使用相當標准的繪圖方法。 我的直覺是邊界/矩形繪制方法正在引起某種問題。

- (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();
}

可能,您必須使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM