简体   繁体   English

简单分配不适用于UIImage * / imageNamed

[英]Simple assignment not working with UIImage* / imageNamed

UIImage* test = [UIImage imageNamed:@"test.png"];
self.image_in_controller = test;

Later in the code when image_in_controller is used, I get EXC_BAD_ACCESS. 稍后在使用image_in_controller的代码中,我得到EXC_BAD_ACCESS。

I set a break point around the time of the assignment. 我在作业时间附近设置了一个断点。 The variable test is getting set just fine.. after the assignment to selfimage_in_controller, test is still okay, but image_in_controller points to 0x0 (not nil). 将变量test设置得很好..分配给selfimage_in_controller后,测试仍然可以,但是image_in_controller指向0x0(不是nil)。

If I run the same code in the simulator it works fine (self.image_in_controller has a valid point address). 如果我在模拟器中运行相同的代码,则可以正常工作(self.image_in_controller具有有效的点地址)。

Any ideas? 有任何想法吗?

Is the property image_in_controller a retained property? 属性image_in_controller是保留的属性吗? If not, you will have to explicitly take ownership of the image with a retain message. 否则,您将必须通过retain消息显式获取映像的所有权。 So one of either: 所以其中之一:

@property(retain) UIImage* image_in_controller;

or 要么

self.image_in_controller = [test retain];

should exist. 应该存在。 The EXC_BAD_ACCESS is often caused by using an object that's been destroyed. EXC_BAD_ACCESS通常是由使用已销毁的对象引起的。 Also, test to make sure that test is not actually nil. 同样,进行测试以确保测试实际上不是零。 You can do this with an assertion: 您可以使用一个断言来做到这一点:

NSParameterAssert(test);

just after test is assigned. 在分配测试之后。 It will let you know if UIImage is not returning a valid object for some reason on the device. 它会告诉您UIImage是否由于某种原因未在设备上返回有效对象。

Finally, 0x0 is the memory address of nil , so you will often see that in the debugger and can (for all intents and purposes) be considered the same as nil, Nil, NULL and 0. 最后,0x0是nil的内存地址,因此您经常会在调试器中看到该地址,并且可以(出于所有目的和目的)将其视为与nil,Nil,NULL和0相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM