在拍摄照片UIImagePickerController时,应用程序崩溃了

时间:2013-01-11 23:53:23

标签: iphone objective-c

当我尝试使用UIMagePickerController拍摄多张照片时,我的应用程序崩溃了。所以,我第一次拍照时一切都很好,第二次在应用程序崩溃时没有任何错误消息。我第一次得到这个:

2013-01-11 16:36:24.178 DoodleStash[26778:907] Received memory warning.

以下是我用来拍照的代码:

- (IBAction)takePhoto:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];

    [self presentViewController:imagePicker animated:YES completion:nil];
    self.isNewDoodle = TRUE;
}}

以下是我用来保存照片的内容

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

[self dismissViewControllerAnimated:YES completion:nil];


if ([mediaType isEqualToString:(NSString *) kUTTypeImage]) {
    self.doodleImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Check to make sure the image is new
    if (self.isNewDoodle == TRUE) {
        UIImageWriteToSavedPhotosAlbum(self.doodleImage, self, @selector(image:finishedSavingWithError:contextInfo:), nil);
    }

    [self performSegueWithIdentifier:@"confirmUploadSegue" sender:self];
}}

真的很感激任何帮助。完整代码在https://gist.github.com/4515007

2 个答案:

答案 0 :(得分:0)

首先对于相机打开尝试下面的行:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

imagePicker.delegate = self;

[self presentViewController:imagePicker animated:YES completion:nil];

点击相机后尝试此操作以保存图像:

              **UPDATED ANSWER**  

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
           UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        if (self.isNewDoodle == TRUE) 
       {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:finishedSavingWithError:contextInfo:), nil);}
    [self performSegueWithIdentifier:@"confirmUploadSegue" sender:self];
    }
   [self dismissViewControllerAnimated:YES completion:nil];
      }

- (void)image:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
    if (error)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Photo Saving Failed"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo Saved" message:@"Saved To Photo Album"  delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}

现在试试这个&检查。

答案 1 :(得分:0)

这不是崩溃,而是内存警告(应用程序在低内存条件下被系统杀死)。很难说为什么你的应用程序在没有看到所有代码的情况下使用了太多内存。您应该使用分析器进行调查。

在给出的代码中,您可以通过仅在写入图像(image:finishedSavingWithError:contextInfo:)之后执行segue并在不再需要之后将doodleImage归零来减少内存使用量(可能在prepareForSegue:sender:中)。如果你重用TakePhoto控制器,后者尤其重要(因为你第二次使用它时,你仍然持有对先前拍摄图像的引用,有效地使记忆效应加倍)。