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

标签 ios cocoa-touch uiimagepickercontroller

有很多关于如何添加按钮以从相机 View 访问照片库的示例,但我似乎找不到任何关于这样做的其他方法。

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

最佳答案

我使用这段代码:

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

关于ios - 使用 UIImagePickerController 从照片库中访问相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321980/

相关文章:

iOS - Swift - UIImage 上的可点击区域

ios - 在 Swift 中校准加速度计

iphone - 如何为 NSLog( @"mytext") 打字和光标焦点设置预定义宏键,并选择在 "mytext "之间进行编辑

iphone - 平滑的变形动画

ios - 如何将相机胶卷图像附加到邮件编辑器

ios - 当我在 iOS 应用程序上收到静默推送通知时,如何启动应用程序或来电 UI?

ios - 根据 URL 前缀执行不同的操作

iphone - 应用程序启动时间限制是否仅限于applicationDidFinishLaunching?

ios - UIButton 不调用 ImageView 中的处理函数

iphone - 更改 UIImageView 顶部和底部的 alpha 值