使用给定的rect在CATiledLayer中绘制图像

时间:2017-10-05 12:41:40

标签: ios objective-c calayer catiledlayer

我尝试绘制并成像到CATiledLayer,图块大小为512x512。唯一的问题是最后的瓷砖在较小的瓷砖内缩小了。提供的图像宽度和高度不是512的倍数。因此,使其为512的倍数所需的剩余部分用黑色填充填充。

How it's seen How i need

我的抽奖代码:

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 个答案:

答案 0 :(得分:0)

我通过删除一行修复此问题。这两个结合起来。

tileRect = CGRectIntersection(self.bounds, tileRect);