OpenFlow 中的 AFGetImageOperation

发布于 2024-11-06 05:32:00 字数 418 浏览 2 评论 0原文

为 OpenFlow 实现 AFGetImageOperation 的正确方法是什么?

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

aImage.imageURL 具有请求的图像 URL,但不确定检索到的图像存储在哪里?

谢谢

What's the correct way to implement AFGetImageOperation for OpenFlow.

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

aImage.imageURL has the requested image URL but unsure where the retrieved image is stored?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-11-13 05:32:00

图像不被缓存。它一次又一次地获取图像。

您可以使用以下方法缓存图像。

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

Images are not cached. It fetches the image again and again.

You can cache the image using following methods..

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文