2011-08-25 78 views
0

是否可以在CATiledLayer頂部繪製UIImage?主要想法是注意視圖上的位置。我使用Apple Library中的PhotoScroller示例,並試圖在tileRect的頂部添加UIImage。任何幫助將不勝感激。在CATiledLayer頂部添加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); 
     } 
    } 
} 
} 

回答

0

可能最好的解決方案是將圖像添加到單獨的視圖中,沒有任何catiledlayer。 您可以添加一個空視圖,並將視圖添加到該視圖的uiimageview中。