我的 iPhone 应用程序从后台返回后崩溃。原因:UIImage问题

发布于 2024-09-24 08:12:08 字数 975 浏览 2 评论 0原文

首先,我想说这个网站太棒了!它帮助我在创建 iPhone 应用程序时完成了很多事情。

现在,我的问题是:

中的 if/else 语句加载图像

当我启动我的应用程序时,我有一个 UIImageView,它根据-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event< /代码>

方法。这些图像的分配如下:

BG.image = someImage;

当然,BG是UIImageView,someImage是带有@property、@synthesis的iVar。 someImage 使用 viewDidLoad 中主包中的图像进行初始化:

- (void)viewDidLoad {

//init stuff from file
someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

[super viewDidLoad];}

我的应用程序运行愉快,根据 touchBegan 加载图像(如上所述),但是!

当我的应用程序发送到后台并返回时,它会在第一次触摸时崩溃。

当我将: 替换

BG.image = someImage

为: 时,

BG.image = [UIImage imageNamed:@"FirstViewBG_5N.png"];

它运行得很愉快?!我认为 someImage 已被刷新或损坏?

我不想这样,因为 imageNamed 方法每次都会从磁盘读取,我认为这会导致性能问题?

我想我的问题很清楚了?就是这样:

1-为什么我的应用程序从后台返回后会崩溃 2-我该如何解决这个问题?

感谢您的所有帮助! 谢谢!

First off, I want to say this site is AWESOME! and it helped me do lots of stuff while creating my iPhone app.

Now, my problem is:

When I launch my app, I have a UIImageView that loads an image depending on an if/else statements in

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

method. These images are assigned as follows:

BG.image = someImage;

of course, BG is the UIImageView, and someImage is an iVar with @property, @synthesis. someImage is initialized with an image from the main bundle in viewDidLoad:

- (void)viewDidLoad {

//init stuff from file
someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

[super viewDidLoad];}

My app runs happily, loading images according to touchBegan (as mentioned), BUT!

When my app is sent to background and comes back, it crashes upon first touch.

When I replaced:

BG.image = someImage

with:

BG.image = [UIImage imageNamed:@"FirstViewBG_5N.png"];

it runs happily?! I think the someImage is flushed or corrupts?

I don't want to leave it like this because imageNamed method reads from disk every time, which will cause performance problems, i think?

I think my question is clear? It is that:

1- Why will my app crash after returning from backgroud
2- How do I solve this?

All your help is appreciated!
Thanks!

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

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

发布评论

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

评论(2

心安伴我暖 2024-10-01 08:12:08

“someImage”已发布。保留它

'someImage' got released. retain it

小女人ら 2024-10-01 08:12:08
  1. 可能是因为图片被公开了。 UIImage 类中的 imageNamed 方法不会为您的 someImage 变量返回 +1 引用计数,因此您不拥有它。
  2. 假设 someImage 是带有保留的属性: self.someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

ps 小心在 dealloc 上释放它;)。

  1. Probably because the image was released. The imageNamed method in UIImage class does not return a +1 reference count for your someImage variable so you don't own it.
  2. Assuming the someImage is a property with retain do: self.someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

p.s. be careful to release it on dealloc ;).

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