编写自己的 iPad 照片库 - 释放错误

发布于 2024-10-10 17:26:40 字数 675 浏览 3 评论 0原文

我想写我自己的相册,就像苹果原来的“Photos.app”一样。 我在 AppDelegate 中创建了一个 UITabbarcontroller,然后创建了一个“ImageViewController”和一个“VideoViewController”。

在“ImageViewController”中,我添加了一个 UIScrollView,然后创建了一个我自己的“PhotoGallery”实例,具有不同的属性,如 imagePerRow、图像、填充等。

对于“PhotoGallery”,我创建了一个新的 Objective-C 类作为“NSObject”的子类,我将所有不同的图像定位为 UIButtons。 然后我添加了另一个函数,它描述了设备方向更改时所有图像的排列。以及 dealloc 函数。就这样。

这个类效果很好,当设备方向改变时也可以重新排列。问题是,如果我在 ios 模拟器中模拟内存警告,第一次 PhotoGallery 会正确释放,但如果我再次模拟警告,我会收到一条错误消息:“[PhotoGallery release]:消息发送到释放实例”。

我认为这是因为 NSObject 的子类,对吗? 然后我将它作为 UIView 进行了测试。有同样的错误。所以知道我不知道该怎么办了。希望您明白问题所在,并给我一些提示。 考虑再次调用 init 函数吗?如何?需要“drawRect”吗?我不知道。

感谢您的时间和帮助, G。

i want to write my own photogallery like the original "Photos.app" from apple.
I´ve created a UITabbarcontroller in the AppDelegate and then an "ImageViewController" and a "VideoViewController".

In the "ImageViewController" i´ve added an UIScrollView and then made an instance of my own "PhotoGallery" with different properties like imagePerRow, images, paddings etc.

For the "PhotoGallery" i´ve created a new objective-c class as a subclass of "NSObject", where i´m positioning all the different images as UIButtons.
Then i´ve added another function which describes the arrangement for all the images when the device orientation has changed. And the dealloc-function. Thats all.

This class works great, also the rearrangement when the device orientation has changed. The problem is, if i simulate a memory warning in the ios-simulator, the first time the PhotoGallery gets correctly dealloc but if i simulate a warning again, i get a error-message: "[PhotoGallery release]: message sent to deallocated instance ".

I thought its because of the subclass as NSObject, right?
Then i´ve tested it as a UIView. With the same error. So know i don´t know what to do anymore. Hope you understand what´s the problem and you would give me some hints on that..
Think about calling the init-function again? How? Need "drawRect"? I´ve no idea.

Thanks for your time and help,
G.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

焚却相思 2024-10-17 17:26:40

您可能没有将保存对 PhotoGallery 的引用的属性设置为零。

IE。您保留对已释放实例的引用,并尝试对其调用 release

不良示例:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
}

safe(r) 示例:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
    photoGallery = nil;

    // or combine both actions if your property attributes are set up to accommodate it:
    // self.photoGallery = nil;
}

在不良示例中,photoGallery 仍保留对现已解除分配的实例的引用,第二个内存警告将尝试向其发送消息。

在 safe(r) 示例中,photoGallery 为 nil,向 nil 发送消息是安全的。

You're probably not setting the property which holds a reference to the PhotoGallery to nil.

ie. You're keeping a reference to a deallocated instance, and attempting to call release on it.

bad example:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
}

safe(r) example:

- (void) didReceiveMemoryWarning
{
    [photoGallery release];
    photoGallery = nil;

    // or combine both actions if your property attributes are set up to accommodate it:
    // self.photoGallery = nil;
}

In the bad example, photoGallery still holds a reference to a now-deallocated instance, and the second memory warning will attempt to send a message to it.

In the safe(r) example, photoGallery is nil, and sending a message to nil is safe.

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