使用UIImagePickerController从照片库中访问相机

时间:2014-04-27 10:04:13

标签: ios cocoa-touch uiimagepickercontroller

有很多关于如何从相机视图中添加按钮来访问照片库的示例,但我似乎无法找到任何相反的方法。

在我的应用程序中,图像的主要来源是库,但我也想让用户使用相机,如果他们真的想要。如果可能的话,我想避免使用两个选项(库和相机)或一些类似的解决方案来呈现动作表的额外步骤。另外,我宁愿没有两个专用按钮。

1 个答案:

答案 0 :(得分:0)

我使用此代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  self.imgPicker  = [[UIImagePickerController alloc] init];

  if(buttonIndex == 0) {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
      self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
      self.imgPicker.allowsEditing = NO;
      self.imgPicker.delegate = self;
      self.imgPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    }
  }

  if(buttonIndex == 1) {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])  {
      self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      self.imgPicker.allowsEditing = NO;
      self.imgPicker.delegate = self;
    }
  }
  [self performSelector:@selector(showPicker) withObject:self afterDelay:0.6f];
}

- (void)showPicker {
  [self presentModalViewController:self.imgPicker animated:YES];
}