使用CATiledLayer显示已以编程方式拆分为切片的大图像

时间:2013-01-18 05:18:08

标签: iphone ios draw tile catiledlayer

我遇到了使用CATiledLayer的问题...我有一张大图像,一张地图,即4726 x 2701.基本上我需要将此图像分解为图块并能够放大查看很多细节,但也能够一直缩放,看到整个地图。使用我当前的实现,如果scrollview的zoomlevel设置为1.0(最大缩放比例),它将完美地工作,但如果缩小,则会错误地替换tile。

这里zoomlevel设置为1.0。地图看起来很完美。 enter image description here

但如果缩放级别一直都是出路(我相信0.2)那么地图就搞砸了。我应该看到整个地图不仅仅是两个瓷砖。

enter image description here

以下是我从大图片中获取图块的方法:

- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col {
     float tileSize = 256.0f;
     CGRect subRect = CGRectMake(col*tileSize, row * tileSize, tileSize, tileSize);
     CGImageRef tiledImage = CGImageCreateWithImageInRect([mapImage CGImage], subRect);
     UIImage *tileImage = [UIImage imageWithCGImage: tiledImage];
     return tileImage;
}

我正在像Apple在PhotoScroller应用程序中那样显示瓷砖。

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat scale = CGContextGetCTM(context).a;

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

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

    for (int row = firstRow; row <= lastRow; row++) {
        for (int col = firstCol; col <= lastCol; col++) {
            UIImage *tile = [self tileForScale:scale row:row col:col];
            CGRect tileRect = CGRectMake(tileSize.width * col, tileSize.height * row,
                                     tileSize.width, tileSize.height);
            tileRect = CGRectIntersection(self.bounds, tileRect);

            [tile drawInRect:tileRect];
        }
    }
}

此外,这里是我设置细节级别的代码,为我的图像返回4:

- (id)initWithImageName:(NSString *)name andImage:(UIImage*)image size:(CGSize)size {
    self = [super initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    if (self) {
        _imageName = [name retain];
        mapImage = [image retain];

        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
        tiledLayer.levelsOfDetail = [self zoomLevelsForSize:mapImage.size];
    }
    return self;
}
- (NSUInteger)zoomLevelsForSize:(CGSize)imageSize {
    int zLevels = 1;
    while(YES) {
        imageSize.width /= 2.0f;
        imageSize.height /= 2.0f;
        if(imageSize.height < 256.0 || imageSize.width < 256.0) break;
        ++zLevels;
    }
    return zLevels;
}

我认为它与瓷砖尺寸和缩放比例有关,但我真的不知道如何解决问题。我似乎无法在Google上找到我的问题的任何解决方案。任何帮助都会很棒! :)

2 个答案:

答案 0 :(得分:1)

你有泄漏 - (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col。

CGImageRef tilesImage = CGImageCreateWithImageInRect([mapImage CGImage],subRect);

不要忘记发布CGImageRef:CGImageRelease(tilesImage);

答案 1 :(得分:0)

我认为这是一个在示例代码中错误设置比例的错误。 我有以下内容:

CGFloat scale = CGContextGetCTM(context).a;
scale = 1.0f / roundf(1.0f / scale);
if (scale == INFINITY) {
    scale = 1.0f;
}