【问题标题】:Generating tiles from a large PNG从大 PNG 生成图块
【发布时间】:2012-12-18 01:06:50
【问题描述】:

我正在寻找使用 CATiledLayer 在 iOS 设备上显示巨大的 PNG 图像。为此,我需要在客户端将较大的图像拆分为图块(100%、50%、25% 和 12.5%)(不能在服务器端创建图块)。

我可以看到有一些库,比如 libjpeg-turbo 可以工作,但是这些是用于 JPEG 的,我需要使用 PNG。

有谁知道我可以在设备上获取大型 PNG(~20Mb)并从中生成图块的方法吗?

任何指针或建议将不胜感激!

谢谢!

【问题讨论】:

    标签: ios image png tiling


    【解决方案1】:

    您可以使用内置的 Core Graphics CGDataProviderCreateWithFilename 和 CGI​​mageCreateWithPNGDataProvider API 打开图像,然后通过执行以下操作创建每个图块:

    const CGSize tileSize = CGSizeMake(256, 256);
    const CGPoint tileOrigin = ...; // Calculate using current column, row, and tile size.
    const CGRect tileFrame = CGRectMake(-tileOrigin.x, -tileOrigin.y, imageSize.width, imageSize.height);
    
    UIGraphicsBeginImageContextWithOptions(tileSize, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, tileFrame, image.CGImage);
    
    UIImage *tileImage = UIGraphicsGetImageFromCurrentImageContext();
    [UIImagePNGRepresentation(tileImage) writeToFile:tilePath atomically:YES];
    UIGraphicsEndImageContext();
    

    您可能还想查看 UIScrollView 类参考下引用的相关示例项目(Large Image Downsizing 和 PhotoScroller)。

    【讨论】:

    • 这确实有效!大图像非常非常慢,但至少可以拆分图像。
    猜你喜欢
    • 2013-02-10
    • 2011-05-23
    • 2011-06-18
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多