【问题标题】:Resize UIImageView after finish drawing完成绘制后调整 UIImageView 的大小
【发布时间】:2012-03-15 12:26:55
【问题描述】:

我正在尝试在我的应用中画线。首先,我将绘图窗口大小设置为我的视图大小。完成绘图后,我想调整 UIImageView 的大小,以便绘图周围没有空格。有没有像 UILabel 中的 sizeToFit 一样自动调整 UIImageView 大小的选项?

   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;

【问题讨论】:

    标签: objective-c ios xcode uiview uiimageview


    【解决方案1】:

    如果我正确理解您的问题,是您想要的 UIView 上的 contentMode 属性(因此是您想要的 UIImageView?这将确定图像的大小以适合视图。

    顺便说一句,您不需要在每个 Core Graphics 函数中调用 UIGraphicsGetCurrentContext()。为什么不将上下文保存在局部变量中:

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    

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

    但是,在我看来,您可以在 UIViewdrawRect 方法中完成所有这些操作,而无需处理创建图像上下文,除非出于某种原因您想保留 UIImage您正在绘制的线条。

    【讨论】:

    • 好的。我会将上下文保存在局部变量中。我正在做的是允许用户在屏幕上绘制一些东西,然后我将插入一个数组。我的数组包含很多图像(照片、用户绘制、邮票)。所以目前绘图的 UIImageView 包含一些透明边框,我不想要它,因为它会影响我选择要编辑的视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2011-01-04
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多