标签:ati 读取 取消 nali 设置代理 tty 显示 source present
一、介绍
图片选择或者拍照功能:
1、选择相册中的图片或是拍照,都是通过UIImagePickerController控制器实例化一个对象,然后通过self.presentViewController方法推出界面显示。但是使用presentViewController的类需要实现UIImagePickerControllerDelegate、UINavigationController两个代理。
2、UIImagePickerController可以通过isSourceTypeAvailable方法来判断设备是否支持照相机/图片库/相册功能。如果支持,可以通过sourceType属性来设置图片控制器的显示类型。类型一共分为3种:PhotoLibrary(照片库)、Camera(相机)、SavedPhotoAlbum(相册)
3、实现协议截图
二、实例
1、使用相册选择步骤:
func chooseImageFromAlbum(){ //判断是否支持要使用的图片库 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //初始化图片控制器 let picker = UIImagePickerController() //设置代理 picker.delegate = self //设置媒体类型 picker.mediaTypes = [kUTTypeImage as String,kUTTypeVideo as String] //设置允许编辑 picker.allowsEditing = true //指定图片控制器类型 picker.sourceType = .photoLibrary //弹出控制器,显示界面 self.present(picker, animated: true, completion: nil) } else{ let alert = UIAlertView.init(title: "读取相册错误!", message: nil, delegate: nil, cancelButtonTitle: "确定") alert.show() } } //实现图片控制器代理方法 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //查看info对象 print(info) //获取选择的原图 let originImage = info[UIImagePickerControllerOriginalImage] as! UIImage //赋值,图片视图显示图片 self.pickerView.image = originImage //图片控制器退出 picker.dismiss(animated: true, completion: nil) } //取消图片控制器代理 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { //图片控制器退出 picker.dismiss(animated: true, completion: nil) }
2、拍照步骤:
func takePhotoFromCamera() { //判断是否支持相机 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { //初始化图片控制器 let picker = UIImagePickerController() //设置代理 picker.delegate = self //设置媒体类型 picker.mediaTypes = [kUTTypeImage as String,kUTTypeVideo as String] //设置来源 picker.sourceType = UIImagePickerControllerSourceType.camera //设置镜头 front:前置摄像头 Rear:后置摄像头 if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front) { picker.cameraDevice = UIImagePickerControllerCameraDevice.front } //设置闪光灯(On:开、Off:关、Auto:自动) picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.on //允许编辑 picker.allowsEditing = true //打开相机 self.present(picker, animated: true, completion: nil) } else{ let alert = UIAlertView.init(title: "找不到相机!", message: nil, delegate: nil, cancelButtonTitle: "确定") alert.show() } }
标签:ati 读取 取消 nali 设置代理 tty 显示 source present
原文地址:http://www.cnblogs.com/XYQ-208910/p/6444346.html