Добавьте UIImage поверх CATiledLayer - PullRequest
0 голосов
/ 25 августа 2011

Можно ли нарисовать UIImage поверх CATiledLayer.Основная идея - отметить позицию на представлении.Я использовал пример PhotoScroller из Apple Library, и я пытаюсь добавить UIImage поверх tileRect.Любая помощь будет оценена.

- (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 голосов
/ 26 августа 2011

Вероятно, лучшим решением было бы добавить изображение в отдельном виде, без какого-либо слоя слоя. Вы можете добавить пустое представление и добавить представление с помощью слоя catiled и представления uiimage к этому представлению.

...