在uiimageview上绘图可以调整uiimage的大小吗?

时间:2014-10-28 16:41:28

标签: ios objective-c uiimageview uiimage uikit

我正在尝试使用UIImage上的触摸事件添加绘图功能(在UIImageView内)。但是,对于某些图像,当我开始绘制时,图像开始缩小,并且当我绘制的图像越多时,图像就变得越来越模糊。我主要使用ray wenderlich的旧绘图教程作为参考,只是更改了值以匹配我的UI。这两张图片展示了我画画时会发生什么:

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

1 个答案:

答案 0 :(得分:0)

有可能,您必须使用

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