In upgrading from iOS6 to iOS7, I think I've found a bug with CATiledLayer, and I'd like to have it verified by the community before submitting to Apple.
The issue is that if you have a UIScrollView with many CATiledLayers within it, the tiles will eventually stop updating.
I have a sample project demonstrating the issue here:
https://github.com/sbudhram/CATiledLayerBug
Please download and run on an iPad running iOS6 vs iOS7.
This project generates 900 CATiledLayers within a UIScrollView, at 3 levels of resolution. As the user zooms in, the tiles update to more refined resolution. The code works on iOS6, but tiles eventually stop updating on iOS7.
I've googled around to see if anyone's had similar problems to this, and found this:
This is different though, because I believe this can happen without a memory warning.
Here is the relevant piece of code in UIViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.backgroundColor = [UIColor yellowColor];
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 4.1;
_scrollView.zoomScale = 2;
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
self.contentView = [[UIView alloc] initWithFrame:_scrollView.bounds];
_contentView.backgroundColor = [UIColor lightGrayColor];
[_scrollView addSubview:_contentView];
CGFloat tileSize = 20.0f;
CGFloat tileSpacing = 4.0f;
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
CATiledLayer *tLayer = [CATiledLayer layer];
tLayer.bounds = CGRectMake(0, 0, tileSize, tileSize);
tLayer.position = CGPointMake(tileSize/2 + i*(tileSpacing+tileSize), tileSize/2 + j*(tileSpacing+tileSize));
tLayer.delegate = self;
tLayer.contentsGravity = kCAGravityResize;
tLayer.contentsScale = [[UIScreen mainScreen] scale];
tLayer.masksToBounds = NO;
tLayer.opacity = 1.0f;
tLayer.backgroundColor = [UIColor colorWithRed:.2 green:.2 blue:.8 alpha:.5].CGColor;
tLayer.levelsOfDetail = 3;
tLayer.levelsOfDetailBias = 3;
tLayer.tileSize = CGSizeMake(1024., 1024.);
[_contentView.layer addSublayer:tLayer];
}
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"Zoom: %f", scrollView.zoomScale);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _contentView;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIImage *drawImage = nil;
if (_scrollView.zoomScale < 2) {
drawImage = [UIImage imageNamed:@"low.png"];
NSLog(@"Drawing - Low");
}
else if (_scrollView.zoomScale < 4) {
drawImage = [UIImage imageNamed:@"med.png"];
NSLog(@"Drawing - Med");
}
else {
drawImage = [UIImage imageNamed:@"high.png"];
NSLog(@"Drawing - Hi");
}
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -layer.bounds.size.height);
CGContextDrawImage(ctx, layer.bounds, [drawImage CGImage]);
}
Here's a snapshot of what happens on iOS7 (everything is filled in on iOS6):
As mentioned by gavi, this now works in 7.1.