简体   繁体   中英

Draw image in CATiledLayer with given rect

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.

怎么看 我需要什么

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];
    }
}

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

tileRect = CGRectIntersection(self.bounds, tileRect);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM