AFGetImageOperation в OpenFlow - PullRequest
       2

AFGetImageOperation в OpenFlow

0 голосов
/ 12 мая 2011

Как правильно реализовать AFGetImageOperation для OpenFlow.

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

aImage.imageURL имеет запрошенный URL-адрес изображения, но не уверен, где хранится полученное изображение?

Спасибо

1 Ответ

0 голосов
/ 30 марта 2012

Изображения не кэшируются.Он получает изображение снова и снова.

Вы можете кэшировать изображение, используя следующие методы ..

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