尽管删除了图像,但 UIImageView 数据仍然存在

UIImageView data persists despite removing image

我有一个应用程序可以在一个 UIImageView 中显示两个全屏图像,一次一个。当用户点击第一张图片时,它会淡出并显示下一张。在显示第二张图像后,它被清除并且没有显示新图像 - 希望内存中没有任何内容。

但是,在 Instruments 中分析应用程序时,我发现每张图像占用大约 12 MB 的内存,但它会累积。显示第一个图像时为 12 MB,显示第二个图像时为 24 MB,然后清除图像后剩余 24 MB。一段时间以来,我一直在努力解决这个问题,看不到这些数据被保留在哪里。

重新审视这段代码会很棒。这是每次用户点击图像时调用的方法:

-(void)displayImages{

    [self.slideshowImage setUserInteractionEnabled:YES];        
    int imageNumber=(int)self.slideshowImage.tag;

    // Fade in first image
    if (imageNumber==0){
        self.slideshowImage.alpha=0;
        [self.slideshowImage setImage:[UIImage imageNamed:[slideshowImages objectAtIndex:imageNumber]]];
        [UIView animateWithDuration:0.3
                         animations:^(void){
                             self.slideshowImage.alpha=1;
                         }
                         completion:^(BOOL finished){
                             self.slideshowImage.tag=imageNumber+1;
                         }];

    // Fade out current image, fade in next image
    }else if (imageNumber<[slideshowImages count]){
        UIImageView *newImage=[[UIImageView alloc] initWithFrame:self.slideshowImage.frame];
        [newImage setImage:[UIImage imageNamed:[slideshowImages objectAtIndex:imageNumber]]];
        newImage.alpha=0;
        [self.view addSubview:newImage];
        [UIView animateWithDuration:0.3
                         animations:^(void){
                             self.slideshowImage.alpha=0;
                         }
                         completion:^(BOOL finished){
                             [UIView animateWithDuration:0.3
                                              animations:^(void){
                                                  newImage.alpha=1;
                                              }
                                              completion:^(BOOL finished){
                                                  [self.slideshowImage setImage:[UIImage imageNamed:[slideshowImages objectAtIndex:imageNumber]]];

                                                  self.slideshowImage.alpha=1;
                                                  [newImage removeFromSuperview];
                                              }];
                         }];
        self.slideshowImage.tag=imageNumber+1;

    // Clear image
    }else{
        [UIView animateWithDuration:0.3
                         animations:^(void){
                             self.slideshowImage.alpha=0;
                         }
                         completion:^(BOOL finished){
                             [self.slideshowImage setTag:0];
                             [self.slideshowImage setImage:nil];
                             [self.slideshowImage setUserInteractionEnabled:NO];
                         }];
    }

}

感谢阅读!

它是 -[UIImage imageNamed:] 的实现,它保留了它加载的所有图像的缓存。

文档中有一个段落准确描述了您的代码中发生的事情:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

但是如果你想有好的性能,使用系统缓存几乎总是更好的。加载 12MB 的图像可能需要超过 16 毫秒,并且会延迟 UI。