[英]Load an UIImage from Documents directory and cache it
我在Documents目錄中有一個UIImage
。 我想加載它並將其緩存在內存中。
要創建和緩存UIImage,我們應該使用+ (UIImage *)imageNamed:(NSString *)name
方法,但是此方法的問題在於圖像應位於應用程序包中。 當我使用其他方法initWithContentsOfFile
,...圖像未緩存。
如何從documents目錄加載UIImage
並將其緩存?
您可以將NSCache
添加到您的AppDelegate中,並具有如下方法:
- (UIImage *)cachedImageWithFilePath:(NSString *)path {
// first check for a hit in the cache
UIImage *cachedImage = [_imageCache objectForKey:path];
if (cachedImage)
return cachedImage;
// load the image from disk and add to the cache
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:path];
[_imageCache setObject:newImage forKey:path];
return newImage;
}
編輯:
有關NSCache的文章 , Apple文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.