• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIImageView动画后如何清理内存

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:51:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的代码: 标题:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
    UIImageView *imageView;
    NSMutableArray *arrayWithImages;
}
- (IBAction)startAnimationid)sender;
- (IBAction)cleanMemoryid)sender;
@end

实现:

#import "ViewController.h"

@implementation ViewController

......

- (IBAction)startAnimationid)sender {
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

    arrayWithImages = [[NSMutableArray alloc] initWithObjects:
                                       [UIImage imageNamed"pic1"],
                                       [UIImage imageNamed"pic2"],
                                       [UIImage imageNamed"pic3"],
                                       [UIImage imageNamed"pic4"],
                                       [UIImage imageNamed"pic5"],
                                       [UIImage imageNamed"pic6"],
                                       [UIImage imageNamed"pic7"],
                                       [UIImage imageNamed"pic8"],nil];
    imageView.animationImages = arrayWithImages;
    imageView.animationDuration = 3;
    imageView.animationRepeatCount = 1;
    [self.view addSubview:imageView];

    [imageView startAnimating];
}

- (IBAction)cleanMemoryid)sender {

    [arrayWithImages removeAllObjects];
    [arrayWithImages release];
    arrayWithImages= nil;

    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;
}
@end

我有 ViewController 和它的 view 有两个按钮。第一个带有 startAnimation Action 的按钮,它创建 UIImageViewNSMutableArray 并在其上启动动画。第二个带有 cleanMemory Action 的按钮,用于清除我在 startAnimation 中创建的所有内容。 当我用 Activity Monitor 仪器启动 Profile 时,当我按下 startAnimation 按钮时,我的程序有 4 mb Real Mem它更改为 16 mb Real Mem 并且在动画之后我按下 cleanMemory 按钮,但它具有相同的 16 mb Real Mem...为什么?我不会将我的内存清理为起始值(4 mb Real Mem)。请问,你能解释一下我哪里有问题吗?



Best Answer-推荐答案


UIImage imageNamed: 缓存图像并按照自己的时间表释放内存。如果你不想缓存,那就是完全控制内存然后直接加载图像,而不是 UIImage imageNamed:

来自 Apple 文档:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

你可以使用

+ (UIImage *)imageWithContentsOfFileNSString *)path

直接加载图片。

来自 Apple 文档:

This method does not cache the image object.

关于ios - UIImageView动画后如何清理内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947389/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap