search

Home  >  Q&A  >  body text

ios - iphone When using UIGraphicsGetCurrentContext in drawRect to draw a rectangle, is there an excess of 1 pixel outside the border?

code show as below

-(void) drawRect:(CGRect)rect{
//CGContextRef context = UIGraphicsGetCurrentContext();
//CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.2);
[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]setFill];
CGRect grayRect = self.frame;
UIRectFill(grayRect);

CGRect cropRect = _intersectionRect;
CGRect intersectionRect = CGRectIntersection(cropRect, grayRect);
[[UIColor clearColor]setFill];
UIRectFill(intersectionRect);
}

In the demo of the blank project, everything is ok

But in my own project, there are extra 1px and borders with different transparency. How to solve it?

我想大声告诉你我想大声告诉你2743 days ago974

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-16 13:20:51

    Problem solved, the values ​​of both rects should be set to int instead of float.

    reply
    0
  • Cancelreply