【问题标题】:Programmatically tiling images for PhotoScroller以编程方式为 PhotoScroller 平铺图像
【发布时间】:2011-12-22 19:40:14
【问题描述】:

我有一个应用程序正在从 S3 存储桶下载大量图片。下载它们后,我会使用以下代码平铺它们。

- (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];

这会如我所愿吗?

【问题讨论】:

    标签: ios catiledlayer


    【解决方案1】:

    是的,那没有用。 initWithCGImage:scale:orientation 仅更改 size 属性报告的大小,它不会对图像进行任何实际缩放。

    我最终选择了这条路线:
    http://iosdevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html

    像魅力一样工作。

    有状态

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 2018-06-29
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 2011-02-16
      相关资源
      最近更新 更多