当我通过分配创建新的 UIImage 时,幕后会发生什么?
我收到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在那一行中,您不是创建一个新的 UIImage 对象;而是创建一个新的 UIImage 对象。您只是创建一个指向旧指针的新指针。如果
在此处旋转图像
意味着对rotatedImage
进行就地修改,那么您实际上也在修改myExistsImage
。如果您想创建一个新的 UIImage 对象来保存旋转后的对象,则需要使用 UIImage
alloc
和init
方法显式执行此操作。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 torotatedImage
, then you're actually modifyingmyExistsImage
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
andinit
methods.使用如下代码:
...您所做的就是复制指针。为了确保
dst
和src
将继续指向有效的UIImage
,您需要调用retain
> 在UIImage
上两次 - 一次用于src
,一次用于dst
。当您不再需要指针时,您应该在UIImage
上调用release
- 当所有指针都消失后,最后一次调用release
将释放UIImage
。您所看到的是UIImage
正在发布,而您仍然有兴趣使用它;那么,您需要保留
它,以防止这种情况发生。With code like:
... all you're doing is copying a pointer. In order to make sure that
dst
andsrc
are going to keep pointing to a validUIImage
you need to callretain
on theUIImage
twice -- once forsrc
and once fordst
. When you no longer need a pointer, you should callrelease
on theUIImage
- when all the pointers have gone away the last call torelease
will deallocate theUIImage
. What you're seeing is that theUIImage
is being released while you're still interested in using it; you need toretain
it, then, to keep that from happening.