在CATiledLayer上添加UIImage

时间:2011-08-25 17:14:06

标签: iphone objective-c uiimage catiledlayer cgrect

是否可以在CATiledLayer之上绘制UIImage。主要思想是注意视图上的位置。我使用了Apple Library中的PhotoScroller示例,我试图在tileRect上添加一个UIImage。任何帮助将不胜感激。

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

    /**** Trying to add UIImage on top of CGRect rect. But not working.****/
CGRect pointRect = CGRectMake(100,100,32,32);
UIImage *image = [UIImage imageNamed:@"map-pointer32.png"];
[image drawInRect:pointRect];

// get the scale from the context by getting the current transform matrix, then asking for
// its "a" component, which is one of the two scale components. We could also ask for "d".
// This assumes (safely) that the view is being scaled equally in both dimensions.
CGFloat initialScale = CGContextGetCTM(context).a;

NSString *value = [NSString stringWithFormat:@"%.3f", initialScale];
CGFloat scale = [value floatValue];

CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
CGSize tileSize = tiledLayer.tileSize;

// Even at scales lower than 100%, we are drawing into a rect in the coordinate system of the full
// image. One tile at 50% covers the width (in original image coordinates) of two tiles at 100%. 
// So at 50% we need to stretch our tiles to double the width and height; at 25% we need to stretch 
// them to quadruple the width and height; and so on.
// (Note that this means that we are drawing very blurry images as the scale gets low. At 12.5%, 
// our lowest scale, we are stretching about 6 small tiles to fill the entire original image area. 
// But this is okay, because the big blurry image we're drawing here will be scaled way down before 
// it is displayed.)
tileSize.width /= scale;
tileSize.height /= scale;

// calculate the rows and columns of tiles that intersect the rect we have been asked to draw
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);



        // if the tile would stick outside of our bounds, we need to truncate it so as to avoid
        // stretching out the partial tiles at the right and bottom edges
        tileRect = CGRectIntersection(self.bounds, tileRect);

        [tile drawInRect:tileRect];




        if (self.annotates) {
            // [[UIColor whiteColor] set];
            CGContextSetLineWidth(context, 6.0 / scale);
            CGContextStrokeRect(context, tileRect);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

可能最好的解决方案是在单独的视图中添加图像,而不使用任何catiledlayer。 您可以添加一个空视图,并将带有catiledlayer的视图和uiimageview添加到该视图中。