Image CATiled层异步图像下载

Image CATiled层异步图像下载,image,asynchronous,background-process,catiledlayer,Image,Asynchronous,Background Process,Catiledlayer,我自定义照片滚动xcode源代码。对于平铺图像,我希望在后台使用nsoperation从web服务器下载图像 该应用程序可以正确下载瓷砖图像,但不会刷新。不确定如何在下载完成后立即刷新平铺图像。如有任何提示,将不胜感激 基本上,我对图像源背后的逻辑略知一二 1) 我决定了最终图像的位置,比如/tmp/tileXX.png 2) 加载磁贴时,我在目标文件夹中查找磁贴 3) 如果它不存在,我从服务器下载了源图像,并使用它绘制互动程序,但也在目标文件夹中绘制,以供将来参考。因此,当它下次绘制时,

我自定义照片滚动xcode源代码。对于平铺图像,我希望在后台使用nsoperation从web服务器下载图像

该应用程序可以正确下载瓷砖图像,但不会刷新。不确定如何在下载完成后立即刷新平铺图像。如有任何提示,将不胜感激





基本上,我对图像源背后的逻辑略知一二

1) 我决定了最终图像的位置,比如/tmp/tileXX.png 2) 加载磁贴时,我在目标文件夹中查找磁贴 3) 如果它不存在,我从服务器下载了源图像,并使用它绘制互动程序,但也在目标文件夹中绘制,以供将来参考。因此,当它下次绘制时,它已经可用,因此不会被下载。 4) 此外,当用户滚动页面或放大/缩小时,它只下载所需的图像。 5) 这避免了在需求出现之前下载所有平铺图像。 6) 因此,甚至不需要后台处理

这可能需要一些时间,最初基于网络连接,但我们无法通过预下载所需的图像来改变这一点

我希望这对类似情况的人有效

湿婆

- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col
{
    //  Step 1
    //  format the target and source folder name using store id, flyer id and page number
    //  format the tile name using folder name and the tile col and row
    //  initiate the background process to download the target file, if required
    tileName = [NSString stringWithFormat:@"%@_%d_%d_%d.png", imageName, (int)(scale * 1000), col + 1, row + 1];
    [self startBackground];

    //  Step 2
    NSString *targetFileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",tileName]];
//    NSLog(@"Return- %@",targetFileName);

    UIImage *image = [UIImage imageWithContentsOfFile:targetFileName];
    return image;
}

- (void)startBackground
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(downloadAsRequired:)
                                        object:tileName];
    [queue addOperation:operation];
    [operation release];
}

- (void)downloadAsRequired:(NSString*)imageTileName
{
    //  Steps
    //  format target file
    //  check if target file exists
    NSString *targetFileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",imageTileName]];
    NSFileManager *fileManager =[NSFileManager defaultManager];
    NSData  *dataFromFile = nil;

    dataFromFile = [fileManager contentsAtPath:targetFileName];
    if (dataFromFile==nil)
    {
        //  file doesn't exist
        NSString *folderName = [NSString stringWithFormat:@"S%@F1/P%d/",[flyer.storeIdentifier stringValue],index + 1];
        NSString *sourceFileName = [NSString stringWithFormat:@"%@%@%@",kLocationTiles,folderName,imageTileName];
        NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:sourceFileName]];
//        UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
        NSLog(@"%@-%@",sourceFileName,targetFileName);
        BOOL fileSaved = [fileManager createFileAtPath:targetFileName contents:imageData attributes:nil];
        if(!fileSaved)
        {
            NSLog(@"failed to copy tile");
        }
        else
        {
            NSLog(@"%@ created",targetFileName);
        }
        [imageData release];
//        [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
    }
    else
    {
        //  file exists, so do nothing
    }
}
- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col
{
    //  Step 1

    UIImage *imageTile=nil;
    tileName = [NSString stringWithFormat:@"%@_%d_%d_%d", imageName, (int)(scale * 1000), col + 1, row + 1];
    NSString *targetFileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@.png",tileName]];

    NSFileManager *fileManager =[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:targetFileName])
    {
        imageTile = [UIImage imageWithContentsOfFile:targetFileName];
    }
    else
    {
        NSString *folderName = [NSString stringWithFormat:@"%@%@/%d/",[id1 stringValue],[id2 stringValue],index + 1];
        NSString *sourceFileName = [NSString stringWithFormat:@"%@%@%@.png",kLocationTiles,folderName,tileName];
        NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:sourceFileName]];

        if (imageData != nil)
        {
            imageTile = [UIImage imageWithData:imageData];
            [fileManager createFileAtPath:targetFileName contents:imageData attributes:nil];
        }
        else
        {
            imageTile = [UIImage imageWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/logo.png"]]];
        }

        [imageData release];
    }

    return imageTile;
}