简体   繁体   English

如何使用objective-c将图像存储在缓存中

[英]How can I store images in cache memory using objective-c

I am having problem when multiple images are being displayed in scrollview (photo Gallery).在滚动视图(照片库)中显示多个图像时遇到问题。 I have fetched images from document directory.我已经从文档目录中获取了图像。 when I display more then 100 images application is getting crashed .当我显示超过 100 张图像时,应用程序崩溃了 so I want to store images in cache memory.所以我想将图像存储在缓存中。

I want to display first 15 images and then other from cache memory.我想显示前 15 张图像,然后显示缓存中的其他图像。 when I scroll the scrollview first 15 from cache will be displayed and then next will be displayed.当我滚动滚动scrollview ,将显示缓存中的第 15 个,然后将显示下一个。

The requirement is to display images in scrollview not in collectionView or other component.要求是在scrollview而不是在collectionView或其他组件中显示图像。

files is array of image paths fetched from documents directory. files是从文档目录中获取的图像路径array

I have fetched all images path and stored in files array .我已获取所有图像路径并存储在files array

int x=2,y=5;
for (int i = 0; i < files.count ; i++)
 {
   if(x<=308)
    {
       NSString *setPath=[NSString stringWithFormat:@"%@/%@",filePath,[files objectAtIndex:i]];
       UIImage* imagePath = [UIImage imageWithContentsOfFile:setPath];
       imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
       imgView.image=imagePath; 
       x+=80;
    }
    else
    {
        x=2;
        y+=77;

        NSString *setPath=[NSString stringWithFormat:@"%@/%@",filePath,[files objectAtIndex:i]];
        UIImage* imagePath = [UIImage imageWithContentsOfFile:setPath];
        imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
        imgView.image=imagePath;
        x+=80;
    }
        [gallaryScrollView addSubview:imgView];
        [self.view addSubview:gallaryScrollView];
         gallaryScrollView.contentSize=CGSizeMake(320, y+120);
 }

Caching isn't going to solve your memory problem.缓存不会解决您的内存问题。 (If anything, using additional memory cache only places more memory strain on the app.) You use cache for performance reasons, not for reducing memory usage. (如果有的话,使用额外的内存缓存只会给应用程序带来更多的内存压力。)您使用缓存是出于性能原因,而不是为了减少内存使用。

The issue is that you're attempting to load image views into scroll view up front.问题是您试图将图像视图加载到滚动视图中。 I'd suggest doing lazy loading of the image views and their associated images.我建议对图像视图及其相关图像进行延迟加载。 And rather than doing it in batches of 15 (or whatever), I'd suggest doing it dynamically in your UIScrollViewDelegate methods:与其分批执行 15 个(或其他),我建议您在UIScrollViewDelegate方法中动态执行此操作:

  • setting delegate for your scroll view;为滚动视图设置delegate

  • implement scrollViewDidScroll which will:实现scrollViewDidScroll它将:

    • remove any image views from the scroll view that are not visible (or near, at the very least)从滚动视图中删除任何不可见(或至少接近)的图像视图

    • add any image views that have scrolled into view and set its image property添加任何已滚动到视图中的image视图并设置其image属性

If you want to marry this with NSCache for caching for performance reasons, you can do that if you want, but I'd suggest focusing on the main manual scroll view delegate implementation first.如果您出于性能原因想将其与NSCache用于缓存,您可以根据需要这样做,但我建议首先关注主要的手动滚动视图委托实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM