当我通过分配创建新的 UIImage 时,幕后会发生什么?

发布于 2024-08-02 10:37:18 字数 694 浏览 1 评论 0原文

我收到 EXC-BAD-ACCESS 类型的错误,其中我通过分配预先存在的 UIImage 的值(如下所示)创建一个新的 UIImage 实例,并且我试图找出问题所在。有一堆代码需要整理,我不知道从哪里开始,所以我不会费心发布源代码——但这可能会帮助我调查你们中的任何一个人可以告诉我到底发生了什么(内存方面)当我通过从另一个分配创建一个新的 UIImage 时?

(in the .h)

UIImage* myExistsImage;

...

@property (retain) UIImage* myExistsImage;

...

-(UIImage*)rotateImage:(UIImage*)imageToRotate;



----------------------------------------------------


(in the .m)
@synthesize myExistsImage;

...

UIImage* myNewImage = [self rotateImage:myExistsImage];


...

-(UIImage*)rotateImage:(UIImage*)imageToRotate
{

UIImage* rotatedImage = imageToRotate;

//Rotate Image Here (details omitted)...

 return rotatedImage;
}

I am getting errors of type EXC-BAD-ACCESS where I create a new UIImage instance by assigning the value of a pre-existing UIImage (as below) and I am trying to figure out what the problem is. There is a heap of code to sort through and I am not sure where to start so I won't bother posting source -- but it might help me in my investigation of any of you could tell me What exactly is going on (memory-wise) when I create a new UIImage by assigning from another?

(in the .h)

UIImage* myExistsImage;

...

@property (retain) UIImage* myExistsImage;

...

-(UIImage*)rotateImage:(UIImage*)imageToRotate;



----------------------------------------------------


(in the .m)
@synthesize myExistsImage;

...

UIImage* myNewImage = [self rotateImage:myExistsImage];


...

-(UIImage*)rotateImage:(UIImage*)imageToRotate
{

UIImage* rotatedImage = imageToRotate;

//Rotate Image Here (details omitted)...

 return rotatedImage;
}

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

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

发布评论

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

评论(2

葮薆情 2024-08-09 10:37:18
UIImage* rotatedImage = imageToRotate;

在那一行中,您不是创建一个新的 UIImage 对象;而是创建一个新的 UIImage 对象。您只是创建一个指向旧指针的新指针。如果在此处旋转图像意味着对rotatedImage进行就地修改,那么您实际上也在修改myExistsImage

如果您想创建一个新的 UIImage 对象来保存旋转后的对象,则需要使用 UIImage allocinit 方法显式执行此操作。

UIImage* rotatedImage = imageToRotate;

In that line, you're not creating a new UIImage object; you're simply creating a new pointer to the old one. If Rotate Image Here implies in-place modifications to rotatedImage, then you're actually modifying myExistsImage as well.

If you want to create a new UIImage object to hold the rotated object, you'll need to explicitly do so with the UIImage alloc and init methods.

凑诗 2024-08-09 10:37:18

使用如下代码:

UIImage* src;
UIImage* dst = src;

...您所做的就是复制指针。为了确保 dstsrc 将继续指向有效的 UIImage,您需要调用 retain > 在 UIImage 上两次 - 一次用于 src,一次用于 dst。当您不再需要指针时,您应该在 UIImage 上调用 release - 当所有指针都消失后,最后一次调用 release 将释放UIImage。您所看到的是 UIImage 正在发布,而您仍然有兴趣使用它;那么,您需要保留它,以防止这种情况发生。

With code like:

UIImage* src;
UIImage* dst = src;

... all you're doing is copying a pointer. In order to make sure that dst and src are going to keep pointing to a valid UIImage you need to call retain on the UIImage twice -- once for src and once for dst. When you no longer need a pointer, you should call release on the UIImage- when all the pointers have gone away the last call to release will deallocate the UIImage. What you're seeing is that the UIImage is being released while you're still interested in using it; you need to retain it, then, to keep that from happening.

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