UIImage caching with Xcode Asset Catalogs

前端 未结 1 1882
醉梦人生
醉梦人生 2021-02-02 16:13

We all know about the mysterious behind-the-scenes caching mechanism of UIImage\'s imageNamed: method. In Apple\'s UIImage Class Reference it says:

相关标签:
1条回答
  • 2021-02-02 16:38

    UPDATE: Cache eviction works fines (at least since iOS 8.3).

    I am running into the same issue (iOS 7.1.1) and I kind of though that @Lukas might be right

    There is a high probability that the mistake is not inside Apple's ... caching but in your .. code.

    Therefore I have written a very simple Test App (view full source below) where I still see the issue. If you see anything wrong with it, please let the me know about it. I know that it really depends on the image sizes. I only see the issue on an iPad Retina.

      @interface ViewController ()
    
      @property (nonatomic, strong) UIImageView *imageView;
      @property (nonatomic, strong) NSArray *imageArray;
      @property (nonatomic) NSUInteger   counter;
    
      @end
    
      @implementation ViewController
    
      - (void)viewDidLoad
      {
          [super viewDidLoad];
    
          self.imageArray = @[@"img1", ...  , @"img568"];
          self.counter = 0;
    
          UIImage *image = [UIImage imageNamed:[self.imageArray objectAtIndex:self.counter]];
          self.imageView = [[UIImageView alloc] initWithImage: image];
          [self.view addSubview: self.imageView];
    
          [self performSelector:@selector(loadNextImage) withObject:nil afterDelay:1];
      }
    
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          NSLog(@"WARN: %s", __PRETTY_FUNCTION__);
      }
    
    
      - (void)loadNextImage{
    
          self.counter++;
          if (self.counter < [self.imageArray count])
          {
              NSLog(@"INFO: %s - %lu - %@",
                    __PRETTY_FUNCTION__,
                    (unsigned long)self.counter,
                    [self.imageArray objectAtIndex:self.counter]);
              UIImage *image = [UIImage imageNamed:[self.imageArray objectAtIndex:self.counter]];
              self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
              [self.imageView setImage:image];
    
              [self performSelector:@selector(loadNextImage) withObject:nil afterDelay:0.2];
          } else
          {
              NSLog(@"INFO: %s %@", __PRETTY_FUNCTION__, @"finished");
              [self.imageView removeFromSuperview];
          }
      }
      @end
    

    Inplace Implementation

    I wrote some code to keep the image asset but load it with imageWithData: or imageWithContentsOfFile: use xcassets without imageNamed to prevent memory problems?

    0 讨论(0)
提交回复
热议问题