2011-12-22 42 views
0

我有一個應用程序正在從S3存儲區下載巨大的圖片。一旦我下載了它們,我用下面的代碼對它們進行平鋪。爲PhotoScroller編程圖像平鋪

- (void)saveTilesOfSize:(CGSize)size 
       forImage:(UIImage*)image 
      toDirectory:(NSString*)directoryPath 
      usingPrefix:(NSString*)prefix 
{ 
    CGFloat cols = [image size].width/size.width; 
    CGFloat rows = [image size].height/size.height; 

    NSLog(@"cols: %f rows: %f", cols, rows); 

    int fullColumns = floorf(cols); 
    int fullRows = floorf(rows); 

    CGFloat remainderWidth = [image size].width - 
    (fullColumns * size.width); 
    CGFloat remainderHeight = [image size].height - 
    (fullRows * size.height); 


    if (cols > fullColumns) fullColumns++; 
    if (rows > fullRows) fullRows++; 

    CGImageRef fullImage = [image CGImage]; 

    int tilecount = 0; 

    for (int y = 0; y < fullRows; ++y) { 
     for (int x = 0; x < fullColumns; ++x) { 
      tilecount++; 
      CGSize tileSize = size; 
      if (x + 1 == fullColumns && remainderWidth > 0) { 
       // Last column 
       tileSize.width = remainderWidth; 
      } 
      if (y + 1 == fullRows && remainderHeight > 0) { 
       // Last row 
       tileSize.height = remainderHeight; 
      } 

      CGImageRef tileImage = CGImageCreateWithImageInRect(fullImage, 
                   (CGRect){{x*size.width, y*size.height}, 
                    tileSize}); 
      NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:tileImage]); 
      NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png", 
           directoryPath, prefix, x, y]; 
      [imageData writeToFile:path atomically:NO]; 

      float prg = tilecount/200.0; 
      [self performSelectorOnMainThread:@selector(setTileProgress:) withObject:[[NSNumber alloc] initWithFloat:prg] waitUntilDone:NO]; 
     } 
    } 
    [self performSelectorOnMainThread:@selector(setTileProgress:) withObject:[[NSNumber alloc] initWithFloat:100.0] waitUntilDone:NO]; 
} 

這可以生成最大的縮放瓦片,但我也需要生成縮放瓦片。我想我可以把UIImage和縮放它像下面的代碼,然後簡單地將該縮放圖像傳遞給修改後的saveTilesOfSize方法以及縮放參數以生成其他縮放圖像(500,250,125)。

UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:0.125 orientation:UIImageOrientationUp]; 

這會做我希望的嗎?

謝謝
有狀態

回答