1. 场景: 在需要执行一个图片组动画时,如何保证动画结束后,图片从内存中消失

    2. 涉及到的核心方法:

      1. imageWithContentsOfFile:
      2. imageNamed:
    1. 总结:

      // 设置animationImages为空后: // 通过imageWithContentsOfFile:加载的图片数组会从内存中消失 - 适用于使用频次较低、大量的图片 // 通过imageNamed:方式加载的图片会持续保留在内存中(使用后在内存中有缓存) - 适用于经常使用,内存占用较小的图片的加载方式;放到Assets.xcassets的图片,默认就有缓存。

    Demo:

    1. #import "ViewController.h"
    2. @interface ViewController ()
    3. @property (nonatomic, strong) UIImageView *imageView;
    4. @end
    5. @implementation ViewController
    6. - (UIImageView *)imageView{
    7. if (!_imageView) {
    8. _imageView = [UIImageView new];
    9. _imageView.backgroundColor = [UIColor darkGrayColor];
    10. _imageView.contentMode = UIViewContentModeScaleAspectFit;
    11. }
    12. return _imageView;
    13. }
    14. - (void)viewDidLoad {
    15. [super viewDidLoad];
    16. // Do any additional setup after loading the view.
    17. [self.view addSubview:self.imageView];
    18. self.imageView.frame = CGRectInset(self.view.bounds, 10, 80);
    19. UIStackView *stackView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 40, 300, 40)];
    20. stackView.axis = UILayoutConstraintAxisHorizontal;
    21. stackView.distribution = UIStackViewDistributionFillEqually;
    22. [self.view addSubview:stackView];
    23. stackView.backgroundColor = [UIColor grayColor];
    24. {
    25. UIButton *btn1 = [UIButton new];
    26. [btn1 setTitle:@"开始1" forState:UIControlStateNormal];
    27. [btn1 addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    28. [stackView addArrangedSubview:btn1];
    29. }
    30. {
    31. UIButton *btn1 = [UIButton new];
    32. [btn1 setTitle:@"开始2" forState:UIControlStateNormal];
    33. [btn1 addTarget:self action:@selector(start2:) forControlEvents:UIControlEventTouchUpInside];
    34. [stackView addArrangedSubview:btn1];
    35. }
    36. {
    37. UIButton *btn1 = [UIButton new];
    38. [btn1 setTitle:@"结束" forState:UIControlStateNormal];
    39. [btn1 addTarget:self action:@selector(end:) forControlEvents:UIControlEventTouchUpInside];
    40. [stackView addArrangedSubview:btn1];
    41. }
    42. }
    43. - (void)start:(UIButton *)sender{
    44. NSMutableArray *images = [NSMutableArray array];
    45. for (NSInteger i = 0; i<8; i++) {
    46. NSString *fileName = [NSString stringWithFormat:@"moto_%03ld",i];
    47. NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"jpeg"];
    48. [images addObject:[UIImage imageWithContentsOfFile:filePath]];
    49. }
    50. self.imageView.animationImages = images;
    51. self.imageView.animationDuration = 8;
    52. self.imageView.animationRepeatCount = 2;
    53. [self.imageView startAnimating];
    54. }
    55. - (void)start2:(UIButton *)sender{
    56. NSMutableArray *images = [NSMutableArray array];
    57. for (NSInteger i = 0; i<8; i++) {
    58. NSString *fileName = [NSString stringWithFormat:@"moto_%03ld",i];
    59. [images addObject:[UIImage imageNamed:fileName]];
    60. }
    61. self.imageView.animationImages = images;
    62. self.imageView.animationDuration = 8;
    63. self.imageView.animationRepeatCount = 2;
    64. [self.imageView startAnimating];
    65. }
    66. - (void)end:(UIButton *)sender{
    67. [self.imageView stopAnimating];
    68. // 设置animationImages为空后:
    69. // 通过imageWithContentsOfFile:加载的图片数组会从内存中消失 - 适用于使用频次较低、大量的图片
    70. // 通过imageNamed:方式加载的图片会持续保留在内存中(使用后在内存中有缓存) - 适用于经常使用,内存占用较小的图片的加载方式;放到Assets.xcassets的图片,默认就有缓存。
    71. self.imageView.animationImages = nil;
    72. }
    73. @end