以编程方式为 PhotoScroller 平铺图像

发布于 2024-12-22 16:24:56 字数 2471 浏览 1 评论 0原文

我有一个应用程序正在从 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];

这会达到我希望的效果吗?

I have an app that is downloading huge pictures from an S3 bucket. Once I have them downloaded, I tile them with the following code.

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

This works lovingly to generate the maximum zoomed tiles but I need to generate the scaled tiles as well. I'm thinking I can take the UIImage and scale it like the following code, then simply pass on that scaled image to the a modified saveTilesOfSize method along with a scale parameter to generate the other scale images (500, 250, 125).

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

Will this do what I hope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-12-29 16:24:56

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

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

就像一个魅力。

有状态的

Yep, that didn't work. initWithCGImage:scale:orientation only changes the size reported by the size property, it doesn't do any actual scaling of the image.

I ended up going this route:
http://iosdevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html

Works like a charm.

Stateful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文