【问题标题】:iPhone app crashiPhone 应用程序崩溃
【发布时间】:2010-10-01 16:59:49
【问题描述】:

我的申请有问题。它在处理图像几次后崩溃,我不知道为什么。 我有一个 TabBar,在 TabBat 的一项中我有一个导航控制器。在该导航的根控制器中,我有两个按钮。一个按钮是从相机或照片库中获取图像,第二个按钮是从应用程序中保存的图像中获取图像。选择图像后,我转到下一个视图控制器 (SelectImageViewController)。

我使用第一个按钮的代码:

- (void) showImagePickerWithSourceType:(NSInteger) sourceType
{
     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
     picker.delegate = self;
     picker.sourceType = sourceType;
     picker.allowsEditing = NO;
     [self presentModalViewController:picker animated:YES];
     [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     UIImage *selectedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerEditedImage"];

     if (selectedImage == nil)
         selectedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

     [[picker parentViewController] dismissModalViewControllerAnimated:YES];

     SelectImageViewController *selectImageViewController = [[[SelectImageViewController alloc] initWithImage:selectedImage] autorelease];
     [self.navigationController pushViewController:selectImageViewController animated:YES];
}

我使用的第二个按钮:

DocumentsViewController *documentsViewController = [[[DocumentsViewController alloc] init] autorelease];
[self.navigationController pushViewController:documentsViewController animated:YES];

DocumentsViewController 是一个带有图像列表的 TableViewController,当我选择一个图像时,我会转到 SelectImageViewController。

在 SelectImageViewController 中我进行一些图像处理,然后转到下一个视图控制器,在那里我进行另一个图像处理。

这是来自 SelectImageViewController 的一些代码:

- (id) initWithImage:(UIImage *) image
{
    self = [super init];
    if (!self) return nil;

    self.originalImage = image;

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

   [NSThread detachNewThreadSelector:@selector(processImage) toTarget:self withObject:nil];
 }

- (void) processImage
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   self.originalImage = [ImageHelper scaleAndRotateImage:self.originalImage toSize:CGSizeMake(1500, 1500)];
   self.processedImage = [ImageHelper scaleImage:self.originalImage maxWidth:320 maxHeight:367];

   [self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO];

   [pool release];
}

- (void) finish
{
   imageView = [[UIImageView alloc] initWithImage:self.processedImage];
   imageView.center = self.view.center;

   [self.view addSubview:imageView];
   [self.view sendSubviewToBack:imageView];

   [NSThread detachNewThreadSelector:@selector(processImage2) toTarget:self withObject:nil];
}

- (void) processImage2
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSArray *corners = [OpenCV processImage:self.processedImage];

  [self performSelectorOnMainThread:@selector(finish2) withObject:nil waitUntilDone:NO];

  [pool release];
}

在 SelectImageViewController 中,我有一个按钮,当我单击它时,我会处理原始图像,然后转到下一个视图控制器。 在 viewDidLoad 方法的最后一个视图控制器中,我再处理一次图像。

我知道所有的图像处理方法都需要大量的内存和一些时间(尤其是我经常使用的原始图像没有缩放),但是当我只从照片库中选择一个图像并运行时,应用程序也会崩溃到 SelectImageViewController。也许我不应该使用导航控制器?有谁知道可能出了什么问题?

感谢您的帮助。

【问题讨论】:

  • 经过快速审查,代码看起来没有任何问题。发布异常。
  • 你在 objc_exception_throw 和 [NSException raise] 处设置断点了吗?这确实可以帮助查明问题。
  • 我不知道异常。几次之后,我开始只收到内存警告,然后有时我什么也没收到,有时我收到内存警告,然后应用程序突然崩溃。我在控制台中只看到“程序接收信号:0”
  • 我还注意到该代码中出现了内存警告: self.originalImage = [ImageHelper scaleAndRotateImage:self.originalImage toSize:CGSizeMake(1500, 1500)];我一直有这样的事情:几次是好的,然后两次我得到内存警告级别 1,然后一次是好的,然后一次我得到内存警告级别 2,然后立即内存警告级别 1,下一次我收到 2 级警告,下次应用崩溃时。

标签: iphone image crash


【解决方案1】:

注意自动释放​​。函数通常返回自动释放对象。您在自动释放池中,因此,当您调用最后一个选择器时,图像可能不存在。尝试使用 de setter 方法分配图像

[self setProcessedImage:[ImageHelper scaleImage:self.originalImage maxWidth:320 maxHeight:367]];

仅当您将变量声明为具有保留或复制的实例属性时,它才会保留图像。 使用 NSZombieEnabled 看看会发生什么。这似乎是一个释放对象的经典问题。

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 2011-06-16
    • 1970-01-01
    • 2012-03-07
    • 2016-01-25
    • 2011-10-06
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多