当我这样做时,内存会发生什么变化?
我试图弄清楚如何在各种视图控制器之间传递 UIImage,而无需复制 UIImage 并增加不必要的内存。使用下面的方法成功传递了图像,但我稍后似乎无法释放此 UIImage,这最终导致我的应用程序因无法解释的 7MB+ 数据而不堪重负。我想知道的是,当我将 UIImage (属于我当前的视图控制器)传递给我将切换到下一个视图控制器,然后释放原始指针时,内存发生了什么?像这样:
-(IBAction)startEditing:(id)sender
{
EditViewController *controller = [[EditViewController alloc] initWithNibName:@"EditViewController" bundle:nil];
controller.delegate = self;
//imageDataPhoto1 is of type UIImage (from camera)
controller.editPhotoData1 = imageDataPhoto1;
[imageDataPhoto1 release];
imageDataPhoto1 = nil;
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
指针 imageDataPhoto1 的 DATA 是否由于在释放之前被分配给视图控制器实例而没有被释放?
I am trying to figure out how to pass a UIImage around between various view controllers without duplicating the UIImage and increasing memory unnecessarily. The image is passed successfully using the method below, but I cannot seem to free up this UIImage later on, which ends up weighing down my app with 7MB+ of data that cannot be accounted for. What I want to know is, what is happening to memory when I pass a UIImage (belonging to my current view controller) to a view controller I will be switching to next, and then release the original pointer? Like this:
-(IBAction)startEditing:(id)sender
{
EditViewController *controller = [[EditViewController alloc] initWithNibName:@"EditViewController" bundle:nil];
controller.delegate = self;
//imageDataPhoto1 is of type UIImage (from camera)
controller.editPhotoData1 = imageDataPhoto1;
[imageDataPhoto1 release];
imageDataPhoto1 = nil;
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];
}
is the DATA of the pointer imageDataPhoto1 not getting released due to it being assigned to the view controller instance before it is released?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
EditViewController
的 .h 文件使用retain
声明editPhotoData1
@property
,那么它将自动保留任何分配给它的对象。您至少需要在EditViewController
的dealloc
方法中显式地release
对象。当然,您应该确保为所有类
release
设置为在 dealloc 方法中保留的所有属性,即:If your
EditViewController
's .h file declares theeditPhotoData1
@property
withretain
, then it will automatically retain any object assigned to it. You'll need torelease
the object explicitly, at the very least ,inEditViewController
'sdealloc
method.As a matter of course, you should make sure to
release
any properties set to retain in your dealloc method, for all classes, i.e.:阅读 UIImage。它在幕后进行了大量的缓存和重新加载,特别是当它从命名文件或路径获取图片时。我不认为复制一个实例实际上会创建第二个副本。请参阅 +imageNamed 和其他人的描述来了解我在说什么:
Read up on UIImage. It does a lot of caching and reloading behind-the-scenes, especially if it gets its pictures from a named file or path. I don't think that duplicating an instance actually creates a second copy. See the description of +imageNamed and others to see what I am talking about: