0

目前,尝试通过 URL 下载图像并以 Apple 商店的 PhotoScroller 应用程序的方式平铺这些图像。最大图像尺寸为 4000 * 4000,为 RGB 格式。我正在使用 256 * 256 的 tilesize 平铺这些图像。平铺图像并将文件写入磁盘需要很长时间。如果可以平铺文件并在 iphone 上加载,则正在寻找更好的方法。总共 4 个 URL 调用,每个 URL 调用获取图像并通过平铺过程并调用 PhotoScroller 应用程序以在 iphone 上启动这些图像。

如果有人遇到过此类问题并对此有所了解,您能否分享一些想法。

-(void) startImageDownloading
{

if (!isImageRequested)
{
    isImageRequested = YES;
    self.responseData = [NSMutableData data];

    URL1 = @"http://www.abc.com/image_id=1&size=1"; 
    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL1]];
    self.connection1= [[[NSURLConnection alloc] initWithRequest:request1 delegate:self] autorelease];

    URL2 = @"http://www.abc.com/image_id=2&size=2"; 
    NSURLRequest *request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL2]];
    self.connection2= [[[NSURLConnection alloc] initWithRequest:request2 delegate:self] autorelease];

    URL3 = @"http://www.abc.com/image_id=3&size=3"; 
    NSURLRequest *request3 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL3]];
    self.connection3= [[[NSURLConnection alloc] initWithRequest:request3 delegate:self] autorelease];


    URL4 = @"http://www.abc.com/image_id=4&size=4";     
    NSURLRequest *request4 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL4]];
    self.connection4= [[[NSURLConnection alloc] initWithRequest:request4 delegate:self] autorelease];

}

}



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

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

for (int y = 0; y < fullRows; ++y) {
    for (int x = 0; x < fullColumns; ++x) {

        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]);
        CGImageRelease(tileImage);
        NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png",directoryPath, prefix, x, y];

        [imageData writeToFile:path atomically:NO];

    }
}    
}
4

2 回答 2

1

由于您的图像是 4000 x 4000 和 RGB,因此下载和平铺在 iPhone 上肯定会花费很长时间,因为它是 CPU 密集型计算。

我已经看到通过从 UIImagePNGRepresentation 切换到 UIImageJPEGRepresentation 来保存文件的实质性改进,因此您可以尝试一下。

对于平铺,解决它的最佳方法是在服务器上而不是在 iphone 上进行。

于 2011-06-29T06:10:30.053 回答
1

似乎您正在重建整个图像,然后为每个图块将其保存到磁盘(并可能再次渲染它),这是一个资源消耗。

试试这个方法:

  1. 将瓷砖保存到磁盘,并并行

  2. 从磁盘读取图块并通过在上下文中绘制它们在内存中构建它们。看看 UIGraphicsBeginImageContext 和 [UIImage drawInRect]。(不确定资源使用是否优于 CGImageCreateWithImageInRect)

  3. 完成后,将其保存到磁盘。请参阅 UIGraphicsGetImageFromCurrentImageContext

如果您在图像变大时遇到内存问题,您可能需要调整#3,即为图块数量添加一个阈值,以便在达到内存限制之前知道何时将其保存到磁盘。

此外,我没有在您的代码中看到您如何渲染要显示的图像,但 setNeedsDisplayInRect 对于仅在指定的矩形上渲染很有用。但是,如果您使用的是 CATiledLayer,这种组合会出现一些问题,请尝试更多地使用它或研究解决方案,直到您完全按照您的意愿得到它。

于 2011-06-29T07:46:50.493 回答