2

以前有人遇到过这个问题吗?我正在使用UIImagePickerController相机通过我的应用程序拍照,我注意到照片在拍摄后没有存储在照片库中。我已经在 iPhone4 的 iOS5 和 iPhone5 的 iOS6 上尝试过,结果相同。这是我的设置方式:

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

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

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

4

1 回答 1

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 并调用 save 方法将其保存到相机胶卷。愿这会有所帮助

于 2013-01-03T07:45:28.580 回答