Draw image in CATiledLayer with given rect

451 views Asked by At

I'm trying to draw and image into CATiledLayer, with tile size 512x512. Only issue is that the last tiles are getting shrunken inside the smaller tiles. The image width and height provided are not multiples of 512. Hence the remaining portion required for making it multiple of 512 is filled with a black padding.

How it's seen How i need

My draw code:

CGContextRef context = UIGraphicsGetCurrentContext();

CGFloat _scaleX = CGContextGetCTM(context).a;
CGFloat _scaleY = CGContextGetCTM(context).d;

CATiledLayer *tiledLayer = (CATiledLayer *) [self layer];
CGSize tileSize = tiledLayer.tileSize;
tileSize.width /= _scaleX;
tileSize.height /= -_scaleY;

NSInteger firstCol = floor(CGRectGetMinX(rect) / tileSize.width);
NSInteger lastCol = floor((CGRectGetMaxX(rect) - 1) / tileSize.width);
NSInteger firstRow = floorf(CGRectGetMinY(rect) / tileSize.height);
NSInteger lastRow = floorf((CGRectGetMaxY(rect) - 1) / tileSize.height);

NSInteger level = self.maxLevelOfDetail + roundf(log2f(_scaleX));
_currentZoomLevel = level;

for (NSInteger row = firstRow; row <= lastRow; row++) {
    for (NSInteger col = firstCol; col <= lastCol; col++) {
        CGRect tileRect = CGRectMake(tileSize.width * col, tileSize.height * row, tileSize.width, tileSize.height);
        UIImage *tileImage = [self.dataSource tiledImageView:self imageTileForLevel:level x:col y:row];
        Tile *tile = [self.tileCache objectForKey:tileCacheKey];
        [tile drawInRect:tile.tileRect blendMode:kCGBlendModeNormal alpha:1];
    }
}
1

There are 1 answers

0
Frankenstein On

I fixed this by removing a line. Which combined two rects.

tileRect = CGRectIntersection(self.bounds, tileRect);