2013-03-11 74 views
0

我自定義照片滾動xcode源。對於平鋪圖像,我想在後臺使用nsoperation從Web服務器下載圖像。CATAIL層異步圖像下載

該應用程序正確下載瓷磚圖像,但未刷新。不確定,如何在下載完成後立即刷新平鋪圖像。任何提示將不勝感激。


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

回答

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)所以,甚至不需要後臺進程。

這可能需要一段時間,最初基於網絡連接,但我們無法通過預先下載所需圖像來更改。

我希望這適用於類似情況下的人。

Siva