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

发布于 2024-09-25 00:10:48 字数 975 浏览 3 评论 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-02 00:10:48

我猜测崩溃是 EXC_BAD_ACCESS (但我猜测是因为您没有发布该信息)。

如果“someImage”是一个实例变量,您应该合成它并使用它的访问器(self.someImage),以便保留或复制它。就目前情况而言,您正在为 someImage 分配一些内容,但当您稍后尝试访问它时,它已经消失了。

I'm guessing the crash is EXC_BAD_ACCESS (but am guessing because you didn't post that information).

If "someImage" is an instance variable, you should synthesize it and use its accessor (self.someImage) so it is retained or copied. As it stands, you're assigning something to someImage but it's gone by the time you're trying to access it later.

静谧幽蓝 2024-10-02 00:10:48

当您执行此操作时:

- (void)viewDidLoad {

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

    [super viewDidLoad];
}

imageNamed 方法将返回一个自动释放的对象,该对象在 viewDidLoad 返回后由垃圾收集器清理。尝试保留它:

    someImage = [[UIImage imageNamed:@"FirstViewBG_5N.png"] retain];

或使用将自动保留它的合成设置器:

    [self setSomeImage:[UIImage imageNamed:@"FirstViewBG_5N.png"]];

或使用 UIImage 的 initWithData 初始值设定项:

    someImage = [[UIImage alloc] initWithContentsOfFile:@"FirstViewBG_5N.png"];

所有功能在功能上都是等效的。 #2 或 #3 最好。

Apple 的“内存管理规则”指南将拯救您的生命:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/文档/uid/20000994-BAJHFBGH

When you do this:

- (void)viewDidLoad {

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

    [super viewDidLoad];
}

The imageNamed method is returning an autoreleased object which gets cleaned up by the garbage collector after viewDidLoad returns. Try either retaining it:

    someImage = [[UIImage imageNamed:@"FirstViewBG_5N.png"] retain];

or using your synthesized setter which will retain it automatically:

    [self setSomeImage:[UIImage imageNamed:@"FirstViewBG_5N.png"]];

or using UIImage's initWithData initializer:

    someImage = [[UIImage alloc] initWithContentsOfFile:@"FirstViewBG_5N.png"];

All are functionally equivalent. #2 or #3 is best.

Apple's "Memory Management Rules" guide will save your life: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH

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