【问题标题】:UIImage view do not have integer height with autolayout, drawn line gets blurryUIImage 视图没有具有自动布局的整数高度,绘制的线变得模糊
【发布时间】:2015-12-28 01:19:29
【问题描述】:

我正在尝试为 iPhone 制作一个简单的绘图应用程序。一切正常,直到我尝试布局。

由于 iPhone 图片格式,我的 Imageview 应该具有 3:4 的固定纵横比。我还将它固定在顶部布局指南和侧面。

我画的线条被扭曲,“流走”

我越靠近视图的底端,这条线就越向上拉。

我在某处读到这可能是由于视图在自动布局之后接收到的高度不是整数,这似乎是真的:

我不想为每个 iOS 设备硬编码矩形。

我怎样才能“四舍五入”这个 0.5 的高度?

这是我的画线方法:

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

    // 1
    UIGraphicsBeginImageContext(tempImageView.frame.size)

    let context = UIGraphicsGetCurrentContext()
    tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: tempImageView.frame.size.width, height: tempImageView.frame.size.height))


    // 2
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y)

    // 3
    CGContextSetLineCap(context, CGLineCap.Round)
    CGContextSetLineWidth(context, brushWidth)
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    // 4b
    CGContextStrokePath(context)

    // 5
    tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    tempImageView.alpha = opacity
    UIGraphicsEndImageContext()

}

我已经尝试过切换抗锯齿,但这并没有帮助,只是让它看起来很糟糕。

【问题讨论】:

    标签: ios iphone swift core-graphics cgcontext


    【解决方案1】:

    使用UIGraphicsBeginImageContext 创建画布时,默认比例为 1。这可能会导致生成的图像不够清晰,无法在视网膜屏幕上显示。

    所以你显然需要指定比例,要么指定你想要的比例,要么指定0,让屏幕决定比例。

    UIGraphicsBeginImageContextWithOptions(tempImageView.frame.size, false, 0.0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2013-05-25
      • 2020-12-06
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多