完成绘制后调整UIImageView的大小

时间:2012-03-15 12:26:56

标签: objective-c ios xcode uiview uiimageview

我试图在我的应用中画线。首先,我将绘图窗口大小设置为我的视图大小。完成绘制后,我想调整UIImageView的大小,以便绘图周围没有空白区域。是否可以选择在UILabel中自动调整UIImageView的大小,如sizeToFit?

   UIGraphicsBeginImageContext(self.vwDesktop.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.vwDesktop.frame.size.width, self.vwDesktop.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    drawImage.userInteractionEnabled = YES;

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么contentMode上的UIView属性(以及UIImageView您想要的是什么?这将决定图片的大小如何适应视图。

另外,您不需要在每个Core Graphics功能中调用UIGraphicsGetCurrentContext()。为什么不直接在本地变量中保存上下文:

CGContextRef ctx = UIGraphicsGetCurrentContext();

并在每次需要引用当前上下文时使用CGContextRef

然而,在我看来,您可以在UIView的{​​{1}}方法中完成所有这些操作而无需处理创建图像上下文,除非出于某种原因您想要保留您正在绘制的线的drawRect

相关问题