質問

I am customizing photo scroller xcode source. for the tiled images i want to download images from a web server using nsoperation in the background.

the app downloads the tiles images properly, but doesn't get refreshed. not sure, how to refresh the tiled images right after the download completed. any hints will be appreciated.


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

Basically, I slightly the logic behind the image source.

1) I decided where I want the final images to be such as /tmp/tileXX.png 2) When I loaded the tile, I looked for the tile at the target folder 3) If it doesn't exist, I downloaded the source image from the server and used it draw the tile, but also at the target folder for future reference. so when it draws next time, it is already available, so wont be downloaded. 4) also it downloads only the required images as the user scroll thru pages or zooms in/out. 5) This avoids downloading all the tiled images, prior to requirement comes up. 6) So there is no background process needed even.

It may take some take time, initially based on the network connection, but we cannot change that, by pre-downloading the required images.

I hope this works for someone in similar scenario.

Siva

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top