有什么方法可以从我的“文档”中获取缓存的 UIImage目录?

发布于 2024-08-03 06:00:37 字数 309 浏览 4 评论 0原文

我知道 -imageNamed: 方法返回一个缓存的 UIImage,但问题是我的图像文件存储在“文档”中,而 -imageNamed: 方法似乎只搜索 Bundle...我目前(不情愿地)使用-imageWithContentsOfFile:从“文档”获取我的图像,但它不一样......放大/缩小包含结果图像的 UIImageView 是不稳定和尴尬的。然而,缩放包含使用 -imageNamed: 创建的图像的同一 UIImageView 看起来非常顺利。所以,再说一遍:如果我无法使用 -imageNamed:,如何从我的“文档”中获取缓存的 UIImage:?

I know that the -imageNamed: method returns a Cached UIImage, but the problem is that my image file is stored in 'Documents', and the -imageNamed: method seems to only search the Bundle... I am currently (reluctantly) using -imageWithContentsOfFile: to get my image from 'Documents' but it is not the same...Scaling up/down a UIImageView containing the resulting image is choppy and awkward. Scaling the same UIImageView containing an image created with -imageNamed: however appears very smooth. So, again: How can I get a cached UIImage from my 'Documents' if I cannot use -imageNamed:?

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

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

发布评论

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

评论(4

吝吻 2024-08-10 06:00:37

我对 rpetrich 提供的答案进行了扩展,并覆盖了 imageName: 方法以添加更多的替换内容。它首先搜索主包,然后查找缓存目录。您当然可以将缓存目录更改为文档目录。

@interface UIImage (CacheExtensions)
+ (UIImage *)imageNamed:(NSString *)name;
+ (void)clearCache;
@end

#import "UIImage+CacheExtensions.h"

static NSMutableDictionary *UIImageCache;

@implementation UIImage (CacheExtensions)

+ (UIImage *)imageNamed:(NSString *)name 
{
    id result;
    if (!UIImageCache)
        UIImageCache = [[NSMutableDictionary alloc] init];
    else {
        result = [UIImageCache objectForKey:name];
        if (result) return result;
    }

    // First, check the main bundle for the image
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];

    result = [UIImage imageWithContentsOfFileimagePath];
    if(result) {
        [UIImageCache setObject:result forKey:name];
        return result;
    }

    // If not found, search for the image in the caches directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cachesImagePath = [[paths lastObject] stringByAppendingPathComponent:name];

    result = [UIImage imageWithContentsOfFile:cachesImagePath];
    if(result) {
        [UIImageCache setObject:result forKey:name];
        return result;
    }

    return nil;
}

+ (void)clearCache
{
    [UIImageCache removeAllObjects];
}

@end

I made an extension of the answer provided by rpetrich and overrode the imageName: method to add more of a drop in replacement. It searches the main bundle first and then looks in the caches directory. You could of course change the caches directory to the document directory.

@interface UIImage (CacheExtensions)
+ (UIImage *)imageNamed:(NSString *)name;
+ (void)clearCache;
@end

#import "UIImage+CacheExtensions.h"

static NSMutableDictionary *UIImageCache;

@implementation UIImage (CacheExtensions)

+ (UIImage *)imageNamed:(NSString *)name 
{
    id result;
    if (!UIImageCache)
        UIImageCache = [[NSMutableDictionary alloc] init];
    else {
        result = [UIImageCache objectForKey:name];
        if (result) return result;
    }

    // First, check the main bundle for the image
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];

    result = [UIImage imageWithContentsOfFileimagePath];
    if(result) {
        [UIImageCache setObject:result forKey:name];
        return result;
    }

    // If not found, search for the image in the caches directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cachesImagePath = [[paths lastObject] stringByAppendingPathComponent:name];

    result = [UIImage imageWithContentsOfFile:cachesImagePath];
    if(result) {
        [UIImageCache setObject:result forKey:name];
        return result;
    }

    return nil;
}

+ (void)clearCache
{
    [UIImageCache removeAllObjects];
}

@end
波浪屿的海角声 2024-08-10 06:00:37

最简单的方法是使用 NSMutableDictionary 存储缓存的图像和清除缓存方法:

@interface UIImage (CacheExtensions)
+ (id)cachedImageWithContentsOfFile:(NSString *)path;
+ (void)clearCache;
@end

static NSMutableDictionary *UIImageCache;

@implementation UIImage (CacheExtensions)
+ (id)cachedImageWithContentsOfFile:(NSString *)path
{
    id result;
    if (!UIImageCache)
        UIImageCache = [[NSMutableDictionary alloc] init];
    else {
        result = [UIImageCache objectForKey:path];
        if (result)
            return result;
    }
    result = [UIImage imageWithContentsOfFile:path];
    [UIImageCache setObject:result forKey:path];
    return result;
}
+ (void)clearCache
{
    [UIImageCache removeAllObjects];
}
@end

注意:您应该从 didReceiveMemoryWarning 调用 +[UIImage clearCache] > 方法。此外,clearCache 将使缓存中的所有对象无效,而不仅仅是未使用的项目;需要一个 UIImage 子类和更复杂的缓存机制来解决这个问题。

The simplest way would be an NSMutableDictionary storing the cached images and a clear cache method:

@interface UIImage (CacheExtensions)
+ (id)cachedImageWithContentsOfFile:(NSString *)path;
+ (void)clearCache;
@end

static NSMutableDictionary *UIImageCache;

@implementation UIImage (CacheExtensions)
+ (id)cachedImageWithContentsOfFile:(NSString *)path
{
    id result;
    if (!UIImageCache)
        UIImageCache = [[NSMutableDictionary alloc] init];
    else {
        result = [UIImageCache objectForKey:path];
        if (result)
            return result;
    }
    result = [UIImage imageWithContentsOfFile:path];
    [UIImageCache setObject:result forKey:path];
    return result;
}
+ (void)clearCache
{
    [UIImageCache removeAllObjects];
}
@end

Note: you should call +[UIImage clearCache] from your didReceiveMemoryWarning method. Also, clearCache will invalidate all objects in the cache, not just unused items; a UIImage subclass and more complicated caching mechanism would be required to remedy this.

时光瘦了 2024-08-10 06:00:37

您可以像 -imageNamed: 一样自行缓存 UIImages 。它只是加载它们,然后保留它们。您也可以使用 NSDictionary 保留它们并实现您自己的 -imageNamed:

但我更担心您在缩放方面遇到的麻烦。您的图像如何进入文档,如何缩放它们,以及您是否测试了捆绑包中存储的相同图像文件?我怀疑 -imageNamed: 与此有任何关系。我更怀疑诸如捆绑应用了一些压缩的事实(尽管我还没有关于为什么这在实践中很重要的理论)、文件中的差异或程序其余部分的差异在扩展期间发生行为(导致磁盘或 CPU 争用)。缓存不太可能与此问题相关。

我会使用仪器进行一些分析,试图找出不稳定的原因。您是否已最大化磁盘、CPU、内存?瓶颈是什么?

You can cache UIImages yourself just as -imageNamed: does. It just loads them, and then holds onto them. You can hold onto them, too, using an NSDictionary and implement your own -imageNamed:

But I'm more concerned about the trouble you're having with scaling. How are your images getting into Documents, how are you scaling them, and have you tested the same image file stored in the bundle? I doubt that -imageNamed: has anything to do with this. I would more suspect things like the fact that the bundle has some compression applied to it (though I don't yet have a theory on why this would matter in practice), differences in the file, or differences in how the rest of the program is behaving during scaling (causing contention on the disk or CPU). Caching is unlikely related to this issue.

I'd do some profiling w/ Instruments to try to find out where the choppiness is coming from. Are you maxing out the disk, CPU, memory? What's the bottleneck?

黒涩兲箜 2024-08-10 06:00:37

编写自己的图像缓存怎么样?您已准备好所有部件,现在您只需封装它并保留已加载图像的记录即可。

What about writing your own image cache? You have all the pieces in place, now you just need to encapsulate it and keep a record of images you've already loaded.

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