第一步

添加协议

UIImagePickerControllerDelegate,UINavigationControllerDelegate

  

 

第二步

添加选择方式

let sexActionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)

        weak var weakSelf = self

        let sexNanAction = UIAlertAction(title: "从相册中选择", style: UIAlertActionStyle.default){ (action:UIAlertAction)in

            weakSelf?.initPhotoPicker()
//填写需要的响应方法 } let sexNvAction = UIAlertAction(title: "拍照", style: UIAlertActionStyle.default){ (action:UIAlertAction)in weakSelf?.initCameraPicker()
//填写需要的响应方法 } let sexSaceAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel){ (action:UIAlertAction)in //填写需要的响应方法 } sexActionSheet.addAction(sexNanAction)
sexActionSheet.addAction(sexNvAction)
sexActionSheet.addAction(sexSaceAction) self.present(sexActionSheet, animated: true, completion: nil)

  

第三步

//MARK: - 相机

    //从相册中选择
func initPhotoPicker(){
let photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.allowsEditing = true
photoPicker.sourceType = .photoLibrary
//在需要的地方present出来
self.present(photoPicker, animated: true, completion: nil)
} //拍照
func initCameraPicker(){ if UIImagePickerController.isSourceTypeAvailable(.camera){
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.allowsEditing = true
cameraPicker.sourceType = .camera
//在需要的地方present出来
self.present(cameraPicker, animated: true, completion: nil)
} else { print("不支持拍照") } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //获得照片
let image:UIImage = info[UIImagePickerControllerEditedImage] as! UIImage // 拍照
if picker.sourceType == .camera {
//保存相册
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
} personImage.image = image self.dismiss(animated: true, completion: nil)
} @objc func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) { if error != nil { print("保存失败") } else { print("保存成功") }
}

  

 

最新文章

  1. Java的多线程机制系列:不得不提的volatile及指令重排序(happen-before)
  2. Git服务器搭建及配置
  3. VIJOS P1037搭建双塔[DP]
  4. Sharepoint 2010 创建栏 计算栏
  5. return false取消手机移动端的默认设置
  6. [系统集成] CI持续集成项目简介
  7. jq获取表单值与赋值代码
  8. 单元测试工具之Xunit
  9. Python 命令行参数解析
  10. fiddler Composer 构建请求
  11. meta的属性详解
  12. 基于Mysql 5.7 GTID 搭建双主Keepalived 高可用
  13. property与x.setter与x.deleter
  14. 问题:计算foldRight(1)(_-_) 与foldLeft(1)(_-_)值不一样
  15. Equal 路由类
  16. python变量类型&字符串的内建函数使用
  17. Sublime Text 2 配置PHP调试环境(在windows环境下)
  18. 自己实现数据结构系列五---BinarySearchTree
  19. C# 调用线程并行上下文穿透-ILogicalThreadAffinative+CallContext
  20. 源码编译安装MySQL8.0

热门文章

  1. Spring Boot 最核心的 25 个注解,都是干货!
  2. Spring Data Jpa接口简介
  3. MySQL查询昨天的数据
  4. Mongodb的集合插入一个字段
  5. Mac下 .bash_profile 和 .zshrc 两者之间的区别
  6. ASP.NET Core WebAPI控制器返回类型的最佳选项
  7. QWebView加载网页
  8. Unable to build: the file dx.jar was not loaded from the SDK folder
  9. SpringBoot入门教程(八)配置logback日志
  10. Chapter 5 Blood Type——14