2

当我尝试使用 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

4

2 回答 2

0

这不是崩溃,而是内存警告(应用程序在内存不足的情况下被系统杀死)。如果不查看所有代码,很难说出您的应用程序使用过多内存的原因。您应该使用探查器进行调查。

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

于 2013-01-13T09:19:38.953 回答
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];
    }
}

现在试试这个并检查。

于 2013-01-12T08:19:47.643 回答