2013-01-11 144 views
2

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

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

回答

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]; 
    } 
} 

现在试试这个&检查。

+0

谢谢@Vishal。你能解释一下这将如何解决这个问题或者它有什么不同? –

+0

尝试了上述解决方案,但似乎没有帮助。 –

+0

我更新我的答案,检查它@Andrei Taraschuk ... – Vishal

0

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

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

相关问题