当我这样做时,内存会发生什么变化?

发布于 2024-08-03 09:01:59 字数 848 浏览 2 评论 0原文

我试图弄清楚如何在各种视图控制器之间传递 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我老 2024-08-10 09:01:59

如果您的 EditViewController 的 .h 文件使用 retain 声明 editPhotoData1 @property,那么它将自动保留任何分配给它的对象。您至少需要在 EditViewControllerdealloc 方法中显式地release对象。

当然,您应该确保为所有类release设置为在 dealloc 方法中保留的所有属性,即:

- (void)dealloc
{
    [editPhotoData1 release];
    ...
    [super dealloc];
}

If your EditViewController's .h file declares the editPhotoData1 @property with retain, then it will automatically retain any object assigned to it. You'll need to release the object explicitly, at the very least ,in EditViewController's dealloc 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.:

- (void)dealloc
{
    [editPhotoData1 release];
    ...
    [super dealloc];
}
殤城〤 2024-08-10 09:01:59

阅读 UIImage。它在幕后进行了大量的缓存和重新加载,特别是当它从命名文件或路径获取图片时。我不认为复制一个实例实际上会创建第二个副本。请参阅 +imageNamed 和其他人的描述来了解我在说什么:

图像命名:

返回关联的图像对象
具有指定的文件名。

<块引用>

  • (UIImage *)imageNamed:(NSString *)名称

参数

name 文件的名称,包括
它的文件扩展名。如果这是
第一次加载图像时,
该方法查找具有以下内容的图像
应用程序中指定的名称
主包。

返回值

指定的图像对象
文件,或者 nil 如果该方法不能
查找指定的图像。

讨论

该方法在系统缓存中查找
对于具有指定的图像对象
名称并返回该对象,如果
存在。如果匹配的图像对象是
尚未在缓存中,此方法
从加载图像数据
指定文件,缓存它,然后
返回结果对象。

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:

imageNamed:

Returns the image object associated
with the specified filename.

  • (UIImage *)imageNamed:(NSString *)name

Parameters

name The name of the file, including
its filename extension. If this is the
first time the image is being loaded,
the method looks for an image with the
specified name in the application’s
main bundle.

Return Value

The image object for the specified
file, or nil if the method could not
find the specified image.

Discussion

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文