【发布时间】: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