iOS--UIimagePickerController

前记

写完知乎日报之后,不知道该学些什么东西,但总的和以后写的项目有关,所以看学长的博客先学习一项相机和相册的调用

UiimagePickerControlller

这是iOS中自带得一个UI类,可以实现对相机及相册的调用和对媒体的处理。

UIImagePickerController 是 iOS 开发中的一个内置视图控制器,用于方便地实现图像和视频的选择和拍摄功能。它提供了一个用户界面,允许用户从设备的相册库中选择照片或视频,或者使用设备的摄像头进行拍摄。
使用 UIImagePickerController 可以实现以下功能:
从相册中选择照片:用户可以浏览设备相册中的照片,并选择其中的一张照片作为所选项。
从相册中选择视频:用户可以浏览设备相册中的视频,并选择其中的一个视频作为所选项。
拍摄照片:用户可以使用设备的摄像头拍摄照片,并将其作为所选项。
拍摄视频:用户可以使用设备的摄像头录制视频,并将其作为所选项。
提供图像编辑功能:UIImagePickerController 还提供了一些简单的图像编辑功能,如裁剪和调整大小等。

我访问他的定义了解到它是UINavigationController的子类,也就是说它是一个Controller,所以基本的操作就是presentviewcontroller ;至于功能是调用相机还是相册由属性决定 ;

viewdidload

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.headimageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biaoqing.png"] ];
    self.headimageview.userInteractionEnabled = YES ;
    UITapGestureRecognizer* tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(presstap)] ;
    self.headimageview.frame = CGRectMake(100, 100, 100, 100) ;
    [self.headimageview addGestureRecognizer:tapgesture] ;//通过手势来添加imageview的点击函数
    [self.view addSubview:self.headimageview] ;
}

presstap

- (void)presstap {
    UIAlertController* alertion = [UIAlertController alertControllerWithTitle:@"please" message:@"select" preferredStyle:UIAlertControllerStyleActionSheet] ;
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gettoCamera] ;
    }] ;
    UIAlertAction* photo = [UIAlertAction actionWithTitle:@"photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gettoPhoto] ;
    }] ;
    UIAlertAction* cancal = [UIAlertAction actionWithTitle:@"cancal" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }] ;
    [alertion addAction:camera] ;
    [alertion addAction:photo] ;
    [alertion addAction:cancal] ;
    
    [self presentViewController:alertion animated:YES completion:nil] ;
    
}

gettoCamera

- (void)gettoCamera {
//初始化Controller
    self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
    //设置代理
    self.imagepickercontroller.delegate = self ;
    //设置sourceType来决定访问相机还是相册
    self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypeCamera ;
    //设置改Controller为全屏
    self.imagepickercontroller.modalPresentationStyle = UIModalPresentationFullScreen ;
    //self.imagepickercontroller.cameraCaptureMode 是 UIImagePickerController 的一个属性,用于设置相机的捕捉模式。在你提供的代码中,设置为 UIImagePickerControllerCameraCaptureModePhoto 表示将相机设置为仅拍摄照片的模式。
    self.imagepickercontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto ;
    //跳转
    [self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}

gettoPhoto

- (void)gettoPhoto {
    //初始化Controller
    self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
    //设置代理
    self.imagepickercontroller.delegate = self ;
    //设置相册照片允许选择
    self.imagepickercontroller.allowsEditing = YES ;
    //设置sourceType访问相册
    self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
    [self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}

切换头像

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    //切换头像
    [self.imagepickercontroller dismissViewControllerAnimated:YES completion:nil] ;
    UIImage* image = [info valueForKey:UIImagePickerControllerEditedImage] ;
    self.headimageview.image = image ;
 }

注意一下,最后一个方法是代理方法,要实现代理才能使用,

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController* imagepickercontroller ;
@property (nonatomic, strong) UIImageView* headimageview ;

@end

UIimagePickerController常用代理方法

1.imagePickerController(_:didFinishPickingMediaWithInfo:):
当用户选择或拍摄媒体后,该方法会被调用。你可以在该方法中获取选择的图像或视频,并对其进行处理。

2.imagePickerControllerDidCancel(_):
当用户取消选择或拍摄媒体时,该方法会被调用。你可以在该方法中执行相应的取消操作。

演示图片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选中的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选中的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其中,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值