4

I have a small piece of code, that draws a circle as CAShapeLayer:

- (void) viewWillAppear:(BOOL)animated {
    CGFloat size = MIN(self.view.frame.size.width, self.view.frame.size.height);
    CAShapeLayer *circleLayer = [CAShapeLayer layer];
    circleLayer.fillColor = [[UIColor redColor] CGColor];
    circleLayer.frame = CGRectMake((self.view.frame.size.width - size) / 2.0 ,(self.view.frame.size.height - size) / 2.0, size, size);
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGPathAddEllipseInRect(pathRef, NULL, CGRectMake(0, 0, size, size));
    circleLayer.path = pathRef;
    CGPathRelease(pathRef);

    [self.view.layer addSublayer:circleLayer];
}

The circle is shown correctly on iPad 2 and in simulator in non-retina mode. When I switch the simulator into retina mode, the circle is not shown at all. Unfortunately, I don't have an actual iPad 3 yet, so I cannot say, if it works on actual device. Can anyone exlain, what I am doing wrong?

Update: I reported this as a bug to Apple. They told me, that the bug is duplicate and the other bug was still open. After a while it got closed, but after the last XCode update it is still reproducible

Update 2: Checked it on a real iPad 3. Works correctly.

Zeroid
  • 101
  • 1
  • 6
  • I have reproduced this. Very odd. – jrturton May 31 '12 at 14:36
  • Your code is fine, it works on all simulators (iPad and iPhone) except iPad retina. I don't have an iPad 3 either so can't test on a device, but this looks like a simulator bug. I do have an app with more complex shape layers and this does work on the iPad retina simulator, so it's an odd one. You should report it to apple, since it is readily reproducible. – jrturton Jun 01 '12 at 06:07
  • Thank you. I'll try to find someone with an iPad 3. – Zeroid Jun 01 '12 at 06:17
  • I'm also trying something like this on the simulator. Apparently if I make the shape smaller it gets drawn... Any update if it's only a simulator's problem? – Nikso Aug 07 '12 at 10:33
  • It would be terrific if you could create an open-radar bug entry for that. – Till Aug 22 '12 at 08:08

1 Answers1

0

Indeed! With your code, if circleLayer's size is 512 or above, the circle won't display on the Simulator's retina iPad...

Now, given that I want to be able to sleep peacefully tonight, here's a workaround that uses CALayer's cornerRadius property to do the trick:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated]; //always call super

    CGFloat size = MIN(self.view.frame.size.width, self.view.frame.size.height);
    NSLog(@"size: %f", size);

    CALayer *circleLayer = [CALayer layer];
    [circleLayer setBackgroundColor:[UIColor redColor].CGColor];
    circleLayer.frame = CGRectMake((self.view.frame.size.width - size) / 2.0 ,(self.view.frame.size.height - size) / 2.0, size, size);

    circleLayer.cornerRadius = size/2;

    [self.view.layer addSublayer:circleLayer];
}

This will display the same circle in the iPad retina Simulator too.

Eric
  • 16,003
  • 15
  • 87
  • 139
  • Also you can use CGFloat scale = [[UIScreen mainScreen] scale]; and like: whiteMaskLayer.frame = CGRectMake(0, 0, imgView.bounds.size.width * scale, imgView.bounds.size.height * scale) – iTux Mar 30 '13 at 13:03