gpt4 book ai didi

ios - 没有缓存的 Afnetworking 离线模式(像 Android 中的 Imageloader 一样保存到文件中)

转载 作者:行者123 更新时间:2023-11-29 02:30:09 24 4
gpt4 key购买 nike

我想使用 afnetworking 方法下载图像并将其显示到 imageview 中。

另一个步骤是我想保存所有图像,以防用户在离线模式下打开应用程序。

SO 上有几篇文章给出了如何在 url header 中使用 UrlCache 和缓存变量的方法。

有android开发的背景,有没有办法让Afnetworking像Imageloader一样工作

Android 中的解决方案? (将文件保存到程序或文件夹中并在离线模式下检索)

这是我在 uicolleciotn View 中显示图像的代码:

    if (![appRecord.thumb_url isEqualToString:@""])
{

[myCell.imageView setImageWithURL:[NSURL URLWithString:appRecord.thumb_url] placeholderImage:[UIImage imageNamed:@"white.png"]];
__weak UIImageView *weakImageView = myCell.imageView;
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:appRecord.thumb_url]];
[myCell.imageView setImageWithURLRequest:request1
placeholderImage:[UIImage imageNamed:@"white.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
if (!strongImageView) return;

[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
strongImageView.image = image;
}
completion:NULL];
}
failure:NULL];

}

根据 luvacu 建议更改代码后:

将此添加到标题:

@property (nonatomic, strong) NSURLRequest *strongRequest1;

和:

        __block UIImageView *weakImageView = myCell.imageView;
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:appRecord.thumb_url]];
__block NSURLRequest *weakRequest1 = request1;
[myCell.imageView setImageWithURLRequest:request1
placeholderImage:[UIImage imageNamed:@"white.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
if (!strongImageView) {
return;
}

[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
strongImageView.image = image;
}
completion:NULL];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSURLRequest *strongRequest = weakRequest1;
UIImageView *strongImageView = weakImageView;
UIImage *cachedImage = [[UIImageView sharedImageCache] cachedImageForRequest:strongRequest];
if (cachedImage) {
[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
strongImageView.image = cachedImage;
}
completion:NULL];
}
}];

使程序脱机后仍然没有图像出现。

最佳答案

AFNetworking 的类别 UIImageView+AFNetworking 包括一个用于每个图像 URL 请求的 NSCache。当请求失败时,尝试为该请求加载缓存图像。

    __block UIImageView *weakImageView = myCell.imageView;
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:appRecord.thumb_url]];
__block NSURLRequest *weakRequest1 = request1;
[myCell.imageView setImageWithURLRequest:request1
placeholderImage:[UIImage imageNamed:@"white.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
if (!strongImageView) {
return;
}

[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
strongImageView.image = image;
}
completion:NULL];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSURLRequest *strongRequest = weakRequest1;
UIImage *cachedImage = [[UIImageView sharedImageCache] cachedImageForRequest:strongRequest]
if (cachedImage) {
[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
strongImageView.image = cachedImage;
}
completion:NULL];
}
}];

此外,您正在加载图像两次。删除第一个:

[myCell.imageView setImageWithURL:[NSURL URLWithString:appRecord.thumb_url] placeholderImage:[UIImage imageNamed:@"white.png"]];

关于ios - 没有缓存的 Afnetworking 离线模式(像 Android 中的 Imageloader 一样保存到文件中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27012357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com