0

写真スクローラーのxcodeソースをカスタマイズしています。並べて表示された画像の場合、バックグラウンドで nsoperation を使用して Web サーバーから画像をダウンロードしたいと考えています。

アプリはタイル画像を適切にダウンロードしますが、更新されません。ダウンロードが完了した直後にタイル画像を更新する方法がわからない。ヒントをいただければ幸いです。


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

4

1 に答える 1

0

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

基本的に、私は画像ソースの背後にあるロジックを少し。

1) /tmp/tileXX.png のように、最終的な画像を配置する場所を決めました 2) タイルをロードしたときに、ターゲット フォルダーでタイルを探しました 3) 存在しない場合は、ソースをダウンロードしましたサーバーから画像を取得し、それを使用してタイルを描画しましたが、将来の参照用にターゲット フォルダーでも使用しました。次回描画するときは、すでに利用可能であるため、ダウンロードされません。4) また、ユーザーがページをスクロールしたりズームイン/ズームアウトしたりすると、必要な画像のみがダウンロードされます。5) これにより、要件が発生する前にすべてのタイル画像をダウンロードする必要がなくなります。6) したがって、バックグラウンド プロセスも必要ありません。

最初はネットワーク接続に基づいて時間がかかる場合がありますが、必要なイメージを事前にダウンロードして変更することはできません。

これが同様のシナリオの誰かに役立つことを願っています。

シヴァ

于 2013-03-19T17:40:58.757 に答える