2011-05-11 56 views
0

爲OpenFlow實現AFGetImageOperation的正確方法是什麼?OpenFlow中的AFGetImageOperation

AFGetImageOperation *getImageOperation = [[AFGetImageOperation alloc] initWithIndex:i viewController:self]; 
getImageOperation.imageURL = [NSURL URLWithString:aImage.imageURL]; 
[loadImagesOperationQueue addOperation:getImageOperation]; 
[getImageOperation release]; 

aImage.imageURL有請求的圖片網址,但不確定檢索到的圖片的存儲位置?

謝謝

回答

0

圖像沒有被緩存。它一次又一次地獲取圖像。

您可以通過以下方法緩存圖像..

-(NSString *) md5String 
{ 
    NSString *md5 = [Utilities md5String:[imageURL absoluteString]]; 

    return md5; 
} 


-(void) storeImage:(UIImage *)image AtPath:(NSString *)path 
{ 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    if([manager fileExistsAtPath:path]) 
    { 
     [manager removeItemAtPath:path error:nil]; 
    } 

    NSData *data = UIImagePNGRepresentation(image); 
    [data writeToFile:path atomically:NO]; 
} 



//TODO: //We need to cehck the expiry date as well.. 
//-(UIImage *) imageFromPath:(NSString *)path Expiry:() 
-(UIImage *) loadImageFromPath:(NSString *)path 
{ 
    UIImage *image = nil; 

    NSFileManager *manager = [NSFileManager defaultManager]; 
    if([manager fileExistsAtPath:path]) 
    { 
     image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 
    } 

    return image; 
} 


-(NSString *) cachedImagePath 
{ 
    NSString *md5 = [self md5String]; 
    NSString *cachedFilePath = [[Utilities applicationCacheDirectory] stringByAppendingPathComponent:md5]; 

    return cachedFilePath; 
} 

- (void)main { 
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 

    if (imageURL) { 


     UIImage *photo = nil; 

     NSString *cachedFilePath = [self cachedImagePath]; 
     UIImage *image = [self loadImageFromPath:cachedFilePath]; 

     if(image) 
     { 
      photo = image; 
     } 
     else 
     { 
      NSData *photoData = [NSData dataWithContentsOfURL:imageURL]; 
      photo = [UIImage imageWithData:photoData]; 
      [self storeImage:photo AtPath:cachedFilePath]; 
     } 

     // Create a UIImage from the imageURL. 

     if (photo) { 
      [mainViewController performSelectorOnMainThread:@selector(imageDidLoad:) 
               withObject:[NSArray arrayWithObjects:photo, [NSNumber numberWithInt:photoIndex], nil] 
               waitUntilDone:YES]; 
     } 
    } else { 
     // Load an image named photoIndex.jpg from our Resources. 
     NSString *imageName = [[NSString alloc] initWithFormat:@"place_holder_bg.png", photoIndex]; 
     UIImage *theImage = [UIImage imageNamed:imageName]; 
     if (theImage) { 
      [mainViewController performSelectorOnMainThread:@selector(imageDidLoad:) 
               withObject:[NSArray arrayWithObjects:theImage, [NSNumber numberWithInt:photoIndex], nil] 
               waitUntilDone:YES]; 
     } else 
      NSLog(@"Unable to find sample image: %@", imageName); 
     [imageName release]; 
    } 

    [pool release]; 
}