- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用NSCache
,正在使用NSCache
来存储图像。我正在UITableView
上显示图像。每当我添加图像时,首先将它们调整大小,然后添加到表中,然后添加到NSCache
中。一切正常。
但是,当我关闭应用程序并再次打开它时,只要应用程序进入后台运行,“我的缓存”将是空的,并且我的应用程序会再次调整图像的大小,然后显示它,因为起初我会看到一个空表。
我不明白为什么会这样。这是NSCache
的预期行为吗?如果是,那么我们如何才能改善用户体验,以使使用不会出现滞后现象。
这是@ipmcc向我建议的代码
类别是我的实体名称(我正在使用coreData)
// A shared (i.e. global, but scoped to this function) cache
static NSCache* imageCache = nil;
// The following initializes the cache once, and only once
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imageCache = [[NSCache alloc] init];
});
// Generate a cache key sufficient to uniquely identify the image we're looking for
NSString* cacheKey = [NSString stringWithFormat: @"%@", category.name];
// Try fetching any existing image for that key from the cache.
UIImage* img = [imageCache objectForKey: cacheKey];
self.imageView.image = img;
// If we don't find a pre-existing one, create one
if (!img)
{
// Your original code for creating a resized image...
UIImage *image1 = [UIImage imageWithData:category.noteImage];
CGSize newSize;
if(image1.size.width == 1080 && image1.size.height == 400)
{
newSize = CGSizeMake(300, 111);
}
dispatch_async(dispatch_get_global_queue(0,0), ^{
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
// Now add the newly-created image to the cache
[imageCache setObject: newImage forKey: cacheKey];
self.imageView.image = newImage;
});
});
}
最佳答案
是的,这样做是出于某种原因,即使没有内存压力,当应用程序进入后台时,它也会立即从缓存中删除数据。要解决此问题,您必须告诉NSCache
不应该丢弃您的数据。
这是解决此问题的方法。
class ImageCache: NSObject , NSDiscardableContent {
public var image: UIImage!
func beginContentAccess() -> Bool {
return true
}
func endContentAccess() {
}
func discardContentIfPossible() {
}
func isContentDiscarded() -> Bool {
return false
}
}
NSCache
中使用此类。
let cache = NSCache<NSString, ImageCache>()
let cacheImage = ImageCache()
cacheImage.image = imageDownloaded
self.cache.setObject(cacheImage, forKey: "somekey" as NSString)
if let cachedVersion = cache.object(forKey: "somekey") {
youImageView.image = cachedVersion.image
}
关于ios - 当应用程序进入后台状态时,NSCache会删除其所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606161/
我有 NSCache,我在其中加载和加载要在 Collection View 中显示的图像。当我必须将数据重新加载到 Collection View 时,我必须清理缓存,否则 Collection V
对于我的应用程序,我必须将一些 HTML 转换为 tableViews 中的 NSAttributedStrings。这是一个 CPU 密集型操作,所以我决定利用 NSCache 有效地将时间减半,因
当用户通过菜单在 View Controller 之间切换时,如果他有足够的内存,我想缓存它们。为此,我想使用 NSCache 来存储许多 UIViewControllers。 但是,我不确定如何告诉
我有一个 collectionView,它一次显示 9 张图像(200x300 像素)。不过,总数可能高达数百。 目前,如果图像在缓存中,我会检查 cellForItem。如果没有,我将从文件系统加载
我正在编写一个需要在缓存中存储一些图像的应用程序。我正在尝试使用 NSCache 来完成它,代码似乎很好,但不要将图像保存在缓存中。我有这段代码: 缓存是全局的,在.h中声明:NSCache *c
我目前使用 NSCache 的一个子类来存储一些图像(值)及其相应的名称(键),当应用程序位于前台时,它似乎工作正常。但是,当我按下主页按钮/用户进入后台并重新进入应用程序时,NSCache 是空的。
我正在研究一种相当简单的缓存机制,目前正在使用 NSCache(出于内存管理的原因)。问题是——缓存是否在两次启动之间持续存在?对我来说好像不行。 我可以很好地将对象放入缓存中,并且可以在应用程序运行
我已经实现了一个列表,其中异步加载了许多大图像。我正在使用 NSCache 的实例来存储这些 UIImage 文件。当我向下滚动时,我加载了越来越多的图像。我设置了它并分析了应用程序的分配情况,我注意
我的应用程序执行某些项目的搜索,并根据获得的每个结果下载图像。它在 tableView 上显示结果,每个结果一个单元格,每个单元格显示相应的图像。 如果已经使用相同的参数进行搜索,我想缓存图像以避免下
我目前使用 NSCache 的子类来存储一些图像(值)及其相应的名称(键),当应用程序位于前台时,它似乎工作正常。但是,当我按下主页按钮/用户进入后台并重新进入应用程序时,NSCache 是空的。 实
NSCache 的一些自动删除策略是什么? Apple的文档没有提到它们,我实验发现NSCache没有响应内存警告。 最佳答案 NSCache 不响应 UIApplicationDidReceiveM
NSCache是一个很少使用的工具,实际上看起来很有用。我创建了一个简单的实验来查看它是如何工作的,看起来它不会在内存不足的情况下自动逐出数据(或者我做错了什么!) - (void)viewDidLo
我注意到当应用程序进入后台时,NSCache 会逐出它的所有对象。那是预期的行为吗?有办法避免吗? 我希望它在设备内存不足时驱逐对象,而不是在应用进入后台时立即驱逐对象。 你知道任何有效的替代方案吗?
谁能举例说明如何使用 NSCache 缓存字符串?或者任何人都有一个很好的解释的链接?好像没找到啊。。 最佳答案 您使用它的方式与使用 NSMutableDictionary 的方式相同。不同之处在于
我正在使用 NSCache 来存储 webData。 webData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:urlStr
我正在编写一个应用程序,需要保留一堆对象的内存缓存,但这不会失控,因此我计划使用 NSCache 来存储所有内容。看起来它会负责清理等工作,这对我来说太棒了。 我还想在启动之间保留缓存,因此我需要将缓
由于 NSCache 已更改为泛型类型 NSCache 现在,这就是我正在执行的代码片段但是,控制台正在打印 -“图像到缓存后”并调度主队列 CustomImageView,但执行的缓存中的图像没有显
我有一个 NSCache 的实例,例如:NSCache *imageCache; 它基本上用于保存一些具有不同 "@keys 的远程“图像”值“。我在开头的全局类中 alloc & init NSCa
我这辈子都想不出如何在 NSCache 中存储与可选项有关的任何内容。我可以理解直接存储一个可选的是没有意义的,但是存储一个数组或字典,其中一些选项可能是可选的,这似乎应该是可能的。 然而,一个简单的
这应该在我的任何给定表格行中显示一个占位符,除非存在该索引路径的图像。当我向下滚动时,先前 indexPath 中的图像会加载默认值而不是释放。我知道之前有人帮助我解决了同样的问题,但这是在我意识到缓
我是一名优秀的程序员,十分优秀!