UIImage view has no integer height with autostart, the drawn line is blurred

I am trying to make a simple drawing application for iPhone. Everything worked fine until I tried to execute the layout.

My image should have a fixed 3: 4 aspect ratio, due to the iPhone image format. I also pinned it to the top guide of the layout and to the sides.

The lines I draw are distorted and flow

flowing lines - screenshot

The closer I get to the lower end of the view, the more the line rises.

I read somewhere that this could be caused by a view getting a height that is not an integer after auto shutdown, which seems true:

rect measurements and autolayout constraints

I don’t want to hardcode the rectangle for every iOS device.

How can I round this 0.5 high?

Here is my line drawing method:

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

I was already trying to switch the antialization, but it didn’t help, it just seemed awful.

+5
source share
1 answer

When creating a canvas with a UIGraphicsBeginImageContext it defaults to 1. This may result in the generated image being not transparent enough to display on the retina screen.

So, you need to specify the scale, obviously, either indicate the desired scale, or specify 0 so that the screen can select the scale.

 UIGraphicsBeginImageContextWithOptions(tempImageView.frame.size, false, 0.0) 
+3
source

Source: https://habr.com/ru/post/1239322/


All Articles