Monday 9 April 2012

Get cache image 2

- (UIImage *) getCachedImage: (NSString *) ImageURLString
{


    NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
   
    [tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];

    NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
    NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
   
    //NSLog(@"filename = %@, uniquepath = %@",filename,uniquePath);
   
   
   
    UIImage *image;
   
    // Check for a cached version
    if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
    {
       // image = [UIImage imageWithContentsOfFile: uniquePath]; // this is the cached image
        NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
        image = [[UIImage alloc] initWithData:imageData];

    }
    else
    {
        // get a new one
        [self cacheImage: ImageURLString];
       
        NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
        image = [[UIImage alloc] initWithData:imageData];
   
       
        //NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:ImageURLString]];
        //image = [[UIImage alloc] initWithData:imageData];
        //image = [UIImage imageWithContentsOfFile: uniquePath];
    }
   
    return image;
   
}

- (void) cacheImage: (NSString *) ImageURLString
{
    NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
   
    NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
    [tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
   
    // Generate a unique path to a resource representing the image you want
    NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7];   // [[something unique, perhaps the image name]];
    NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
   
    NSLog(@"%@",uniquePath);
   
   
    // Check for file existence
    if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
    {
        // The file doesn't exist, we should get a copy of it
       
        // Fetch image
        NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
        [data writeToFile:uniquePath atomically:YES];
       
    }
   
   
}

-(void) removeFromCache:(NSString *) ImageURLString{
   
    NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
    [tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
   
    // Generate a unique path to a resource representing the image you want
    NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7];   // [[something unique, perhaps the image name]];
    NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
   
    //NSLog(@"%@",uniquePath);
   
    // Check for file existence
    if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:uniquePath error:nil];
    }

}

No comments:

Post a Comment