title: Draw image in CATiledLayer with given rect
body: <p>I'm trying to draw and image into CATiledLayer, with tile size 512x512. Only issue is that the last tiles are getting shrunken inside the smaller tiles. The image width and height provided are not multiples of 512. Hence the remaining portion required for making it multiple of 512 is filled with a black padding.</p>
  
  <p><a href="https://i.stack.imgur.com/CFwJm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CFwJm.jpg" alt="How it&#39;s seen"></a>
  <a href="https://i.stack.imgur.com/di1wF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/di1wF.png" alt="How i need"></a></p>
  
  <p>My draw code:</p>
  
  <pre><code>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 &lt;= lastRow; row++) {
      for (NSInteger col = firstCol; col &lt;= 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];
      }
  }
  </code></pre>