iosimagescalecatiledlayer

iOS CATiledLayer and TilingView scale problems?


I am using the TilingView from the Apple PhotoScroller example to tile some images. This works great for most of my images, but I have a few get weird scale values. I set the level of detail to 4. My images are all scaled at different values, 100,50,25,12.5 scales then tiled 256x256 at those levels.

In TilingView drawRect method, the scale I get here must be one of 4 values and normally is 1.0,0.50,0.25,0.125. Since I store my images off based on these scale values when I get a weird scale value it breaks and cannot load the images. For example I have an image that at .50 scale the actual value I get is 0.499798.

Any ideas whats going on here? If I tell the CATiledLayer to have 4 levels of detail, how do I end up with these weird values?

CGFloat scale = CGContextGetCTM(context).a;
NSLog(@"scale = %f",scale);
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
CGSize tileSize = tiledLayer.tileSize;

How can I ensure that the image size I pass actually will return me one of the 4 scales 100,50,25,12,5 for any image size I specify?


Solution

  • There are several bugs in that sample's code, one of which involves proper rounding of those scale values, which leads to the issue you are seeing. But there are also other subtle issues. Please have a look at this question, where those issues (and the fixes) are described in more detail.