0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"View", nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 100, 80, 80)];
NSString *imagePath = [NSString stringWithFormat:@"%@", [Array objectAtIndex:x]];
UIImage *bkgImg = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagePath ofType:@"png"]];
imageView.image = bkgImg;
[bkgImg release];
[alert addSubview:imageView];
[imageView release];
[alert show];
[alert release];    

这就是我用来创建警报视图的代码。目前,我已经设置好了,所以如果用户按下其中一个按钮,它将加载一个新的视图控制器。在我向 UIAlertView 添加子视图之前,它工作正常。现在,每当它动画到新屏幕时,它只会使程序崩溃。我对 iPhone 开发相当陌生,任何帮助将不胜感激。

4

1 回答 1

1

你正在做:

UIImage *bkgImg = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagePath ofType:@"png"]];
...
[bkgImg release];

但是+imageWithContentsOfFile返回一个自动释放的 UIImage 实例,所以你应该自己释放它。可能发生的事情是 NSAutoreleasePool 向一个-release已经被释放的对象发送一个,导致应用程序稍后崩溃。

我建议好好看看http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html(或等效的 iPhone 文档,如果存在的话)。

于 2010-05-31T10:13:37.633 回答