UIImagePickerController相机不保存照片库中的照片

时间:2013-01-03 07:39:46

标签: iphone ios cocoa-touch uiimagepickercontroller

以前有没有人遇到这个问题?我正在使用UIImagePickerController相机为我的应用程序拍照,我注意到照片在拍摄后没有存储在照片库中。我已经在iOS5上使用iPhone4和iOS6以及iPhone5进行了尝试,结果相同。以下是我如何设置它:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}

imagePickerController.delegate = self;
[self.navigationController presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];

好像UIImagePickerController应该隐式保存拍摄到用户照片库的照片?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:12)

请尝试以下代码。

-(IBAction)useCamera
{
 if ([UIImagePickerController isSourceTypeAvailable:
      UIImagePickerControllerSourceTypeCamera])
 {
  UIImagePickerController *imagePicker =
  [[UIImagePickerController alloc] init];
  imagePicker.delegate = self;
  imagePicker.sourceType =
  UIImagePickerControllerSourceTypeCamera;
  imagePicker.mediaTypes = [NSArray arrayWithObjects:
                            (NSString *) kUTTypeImage,
                            nil];
  imagePicker.allowsEditing = NO;
  [self presentViewController:imagePicker animated:YES completion:nil];
  newMedia = YES;
 }
}

#pragma mark- camera picker delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 NSString *mediaType = [info
                        objectForKey:UIImagePickerControllerMediaType];

 [self dismissViewControllerAnimated:YES completion:nil];

 if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
  UIImage *image = [info
                    objectForKey:UIImagePickerControllerOriginalImage];

  [imageView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth)];
  imageView.image = image;
  if (newMedia)
   UIImageWriteToSavedPhotosAlbum(image,
                                  self,
                                  @selector(image:finishedSavingWithError:contextInfo:),
                                  nil);
 }
 else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
 {
  // Code here to support video if enabled
 }
}

-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
 if (error) {
  UIAlertView *alert = [[UIAlertView alloc]
                        initWithTitle: @"Save failed"
                        message: @"Failed to save image"\
                        delegate: nil
                        cancelButtonTitle:@"OK"
                        otherButtonTitles:nil];
  [alert show];
 }
}

从相机中选取照片并将其存储到ImageView并调用保存方法将其保存到相机胶卷。  愿这会有所帮助

答案 1 :(得分:-1)

您是否实现了UIImagePickerControllerDelegate方法的方法?

最重要的方法是在用户完成拍摄或选择图像时调用的didFinishPickingMediaWithInfo

link:http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_%28Xcode_4%29