【发布时间】:2012-09-29 16:47:26
【问题描述】:
我是 Objective-C 编程的新手,我遇到了典型的内存问题。我必须做一个基于导航控制器的应用程序,并且在传递少量视图时(使用推送视图控制器),加载 100 个图像的动画。在模拟器中效果很好,但在手机上却不行……我打开了不同的动画,然后它就关闭了。我正在使用 arc 来避免这种情况,但它似乎不起作用。我也尝试禁用弧并手动释放 UIImageView 但它甚至很快崩溃。以下是其中一种视图的示例:
//.h
@interface Gegant_nou : UIViewController {
IBOutlet UIImageView *ImageViewGegant;
}
@property (nonatomic, strong) UIImageView* ImageViewGegant;
//.m
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *rigthButton = [[UIBarButtonItem alloc] initWithTitle:@"Detalls" style:UIBarButtonItemStyleBordered target:self action:@selector(canviarDetalls)];
self.navigationItem.rightBarButtonItem = rigthButton;
[rigthButton release];
ImageViewGegant.animationImages =@
[[UIImage imageNamed:@"0001.png"],
[UIImage imageNamed:@"0002.png"],
. load all the images
.
[UIImage imageNamed:@"0099.png"],
[UIImage imageNamed:@"0100.png"]];
ImageViewGegant.animationDuration = 4;
ImageViewGegant.animationRepeatCount = 0;
[ImageViewGegant startAnimating];
[self.view addSubview:ImageViewGegant];
self.title = @"Gegant nou";
[ImageViewGegant release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload{
[super viewDidUnload];
[ImageViewGegant release];
}
知道为什么会发生吗?谢谢你帮助我!
【问题讨论】:
-
1.哪一行抛出异常? 2.
-viewDidUnload在 iOS6 下被弃用,你应该避免使用它(它永远不会被调用)。 3. 为什么不使用循环初始化图像数组? -
关于使用 ImageNamed 加载与 initwtihcontentsofffile 的内存使用存在一些争论。有关其他选项stackoverflow.com/questions/6566827/… 的一些有用信息,请参阅此帖子
-
@holex 我应该用什么来代替 viewDidUnload?解除分配?
-
@user1600400,根据文档,您应该使用
-didReceiveMemoryWarning方法来释放您不再需要的所有内容。 -
好的@holex,我会的,谢谢!
标签: objective-c memory-management memory-leaks automatic-ref-counting uiviewanimation