[Objective-C]UIImagePickerController使用教程

UIImagePickerController是iOS平台上的一个系统图像选择器,用于从设备的照片库或相机中选择或拍摄图像。本文将介绍如何使用UIImagePickerController来选择和展示图像。

步骤一:导入头文件

在你的ViewController中引入和这两个协议,并且在.h文件中添加一个UIImagePickerController对象:

@interface MyViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

步骤二:初始化UIImagePickerController

在你的ViewController的.m文件中,重写viewDidLoad方法并初始化UIImagePickerController对象:

- (void)viewDidLoad {
   [super viewDidLoad];
   
   self.imagePicker = [[UIImagePickerController alloc] init];
   self.imagePicker.delegate = self;
   self.imagePicker.allowsEditing = YES;
}

这里设置allowsEditing属性为YES,表示可以对所选图像进行编辑,例如裁剪、旋转等操作。

步骤三:实现打开UIImagePickerController

在需要打开UIImagePickerController的地方,例如点击一个按钮时,在UIButton的Action方法中添加以下代码:

- (IBAction)selectImage:(id)sender {
   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  
   UIAlertAction *choosePhotoAction = [UIAlertAction actionWithTitle:@"Choose Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
       [self presentViewController:self.imagePicker animated:YES completion:nil];
   }];
   [alertController addAction:choosePhotoAction];

   UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       [self presentViewController:self.imagePicker animated:YES completion:nil];
   }];
   [alertController addAction:takePhotoAction];

   UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
   [alertController addAction:cancelAction];

   [self presentViewController:alertController animated:YES completion:nil];
}

这里使用UIAlertController来创建一个弹出菜单,让用户选择是从照片库中选择图像还是直接拍摄照片。在选择之后,设置UIImagePickerController的sourceType属性为所选类型,然后通过presentViewController方法打开UIImagePickerController。

步骤四:实现UIImagePickerControllerDelegate

在你的ViewController的.m文件中,实现UIImagePickerControllerDelegate协议中的方法来获取所选的图像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   self.imageView.image = chosenImage;
  
   [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
   [picker dismissViewControllerAnimated:YES completion:nil];
}

在didFinishPickingMediaWithInfo方法中,获取编辑过的UIImage对象,并将其设置到实例中的UIImageView上。最后调用dismissViewControllerAnimated方法,关闭UIImagePickerController。

总结

以上就是如何使用UIImagePickerController来选择和展示图像的完整教程。希望对你有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struct VideoPicker: UIViewControllerRepresentable { @Environment(.presentationMode) private var presentationMode let sourceType: UIImagePickerController.SourceType // let onImagePicked: (UIImage) -> Void let onURLPicked: (URL) -> Void final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @Binding private var presentationMode: PresentationMode private let sourceType: UIImagePickerController.SourceType private let onURLPicked: (URL) -> Void init(presentationMode: Binding<PresentationMode>, sourceType: UIImagePickerController.SourceType, onURLPicked: @escaping (URL) -> Void) { presentationMode = presentationMode self.sourceType = sourceType self.onURLPicked = onURLPicked } func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { // let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage // onImagePicked(uiImage) if let url = info[.mediaURL] as? URL{ onURLPicked(url) } presentationMode.dismiss() } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { presentationMode.dismiss() } } func makeCoordinator() -> Coordinator { return Coordinator(presentationMode: presentationMode, sourceType: sourceType, onURLPicked: onURLPicked) } func makeUIViewController(context: UIViewControllerRepresentableContext<VideoPicker>) -> UIImagePickerController { let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = context.coordinator picker.mediaTypes = ["public.movie"] return picker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<VideoPicker>) { } }这段代码获取的url中绝对路径不准确
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值