【问题标题】:Cache Processed (e.g. blurred or edited) Images缓存处理(例如模糊或编辑)图像
【发布时间】:2014-12-16 01:38:51
【问题描述】:

是否有最佳实践或库可以帮助在 iOS 中缓存已处理的图像(即在应用运行时创建的图像)?我将 SDWebImage 用于我下载的图像,但在应用程序的不同位置我模糊或以其他方式处理其中一些图像。我想将处理后的图像存储在缓存中,以便我可以轻松访问它们,而不是每次用户打开该图像时重新处理。最好的方法是什么?

谢谢!

【问题讨论】:

    标签: ios image caching


    【解决方案1】:

    答案似乎是使用 NSCache。这很简单。我最终得到了一个 NSCache 的子类,以确保处理内存警告。

    NATAutoPurgeCache 的实现(主要基于 StackOverflow 上的其他帖子)

    @implementation NATAutoPurgeCache
    
    + (NATAutoPurgeCache *)sharedCache
    {
        static NATAutoPurgeCache *_sharedCache = nil;
    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedCache = [[self alloc] init];
    
        });
    
        return _sharedCache;
    }
    
    - (id)init
    {
        self = [super init];
        if (self) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
    
    @end
    

    并在需要图像时使用它:(在这种情况下存储模糊图像)

                UIImage* blurImage = [myCache objectForKey:@"blurred placeholder image"];
    
                if (!blurImage)
                {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
                    UIImage* blurImage = self.activityPic.image;
                    blurImage= [blurImage applyLightEffect];
    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.activityPic.image = blurImage;
                        });
    
                        [myCache setObject:blurImage forKey:@"blurred placeholder image"];
                    });
                }
                else {
                    self.activityPic.image = blurImage;
                }
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 2013-11-01
      • 2013-10-03
      • 2016-06-07
      • 2021-02-07
      • 2019-02-27
      • 2015-10-08
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多