配置:

如果是相机使用,需要在info.plist文件增加使用前应用程序说明;相机使用也是如此。(第二个是CALENDARS权限,打错了;图片是Photo Library Usage Description)

从相册选择图片:

使用UIImagePickerController进行图片选择需要实现UIImagePickerControllerDelegate, UINavigationControllerDelegate这两个代理。因为图片选择视图控制器属于系统内定义的NavigationController。

import UIKitclass ImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {var buttonSelect: UIButton!var imageView: UIImageView!private func initView(){buttonSelect = UIButton(type: .system)buttonSelect.setTitle("Select", for: UIControl.State.normal)buttonSelect.frame = CGRect(x: ((self.view.frame.width / 2) - 35), y: (self.view.frame.width - 200), width: 70, height: 30)buttonSelect.addTarget(self, action: #selector(selectPicture), for: UIControl.Event.touchDown)self.view.addSubview(buttonSelect)imageView = UIImageView(frame: CGRect(x: ((self.view.frame.width / 2) - 120), y: ((self.view.frame.height / 2) + 50), width: 240, height: 240))imageView.image = UIImage(named: "taylor")self.view.addSubview(imageView)}override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.initView()}@objc func selectPicture(){// 设置源为相机if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {//创建图片选择控制器let pickerVC = UIImagePickerController()pickerVC.delegate = self// 设置源为相机pickerVC.sourceType = UIImagePickerController.SourceType.photoLibraryself.present(pickerVC, animated: true, completion: nil)} else {print("------> ERROR")}}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {print("------> INFO:\(info)")let image: UIImage!image = info[.originalImage] as? UIImageself.imageView.image = imagepicker.dismiss(animated: true, completion: nil)}}

需要说明的是,info这个对象是个数组,里面存放的是原始图片与修改后的图片。例如info[.originalImage]是原始图片。

从相机选择图片:

import UIKitclass ImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {var buttonSelect: UIButton!var imageView: UIImageView!private func initView(){buttonSelect = UIButton(type: .system)buttonSelect.setTitle("Select", for: UIControl.State.normal)buttonSelect.frame = CGRect(x: ((self.view.frame.width / 2) - 35), y: (self.view.frame.width - 200), width: 70, height: 30)buttonSelect.addTarget(self, action: #selector(selectPicture), for: UIControl.Event.touchDown)self.view.addSubview(buttonSelect)imageView = UIImageView(frame: CGRect(x: ((self.view.frame.width / 2) - 120), y: ((self.view.frame.height / 2) + 50), width: 240, height: 240))imageView.image = UIImage(named: "taylor")self.view.addSubview(imageView)}override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.initView()}@objc func selectPicture(){if UIImagePickerController.isSourceTypeAvailable(.camera) {let pickerVC = UIImagePickerController()pickerVC.delegate = selfpickerVC.sourceType = UIImagePickerController.SourceType.cameraself.present(pickerVC, animated: true, completion: nil)} else {print("------> ERROR")}}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {print("------> INFO:\(info)")let image: UIImage!image = info[.originalImage] as? UIImageself.imageView.image = imagepicker.dismiss(animated: true, completion: nil)}}

闪光灯开启:

pickerVC.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On

使用后置相机:

if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){pickerVC.cameraDevice = UIImagePickerControllerCameraDevice.Front
}

效果:


Swift使用UIImagePickerController 从相册选择图片、从相机选择图片相关推荐

  1. 实现炫酷的获取本地图片和相机拍照图片-自定义组件

    IOS中有封装好的选择图片后长按出现动画删除效果,效果如下 而Android找了很久都没有找到有这样效果的第三方组件,最后懒得找了还是自己实现这效果吧 选择图片后还可对图片进行剪裁 当然,代码中还有很 ...

  2. Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片

    Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片 设备图片库和照相机是图像的两个重要来源,使用UIKit中提供的图像选择器UIImagePic ...

  3. Android的实现既能相册选择,拍照选择,点击每张图片又能放大查看!

    最近很长一段时间没有更新博客了实在是比较忙最近需要使用一个功能:选择本机相册或者拍照返回图片显示到九宫格中,并且可以点击九宫格每一张放大查看,滑动等功能! 在网上也看到一些大神写的演示和第三方库,不过 ...

  4. android调取手机相册或打开相机选择图片并显示

    作为一个android小白,自己想尝试写一个小项目,因此写个小博客记录一下自己的开发历程.这一篇记录自己学习调取手机相册以及打开相机选择图片并显示 示例是采用PopupWindow弹出底部菜单,选择相 ...

  5. android 微信相册功能,Android仿微信选择图片和拍照功能

    本文实例为大家分享了 Android微信选择图片的具体代码,和微信拍照功能,供大家参考,具体内容如下 1.Android6.0系统,对于权限的使用都是需要申请,选择图片和拍照需要申请Manifest. ...

  6. 小程序生成图片保存到系统相册_iSee图片专家下载|iSee图片专家 3.930 官方版

    下面点击下载安装,最新更新的这个版本对于很多用户来说,体验感觉还是可以的.虽然有一些小问题没有解决,但是大体上还是符合用户使用需求.尤其是针对之前版本的修改,就比较OK的感觉. iSee图片专家软件特 ...

  7. Android apps 拍立知-功能实现2(相机/选择相册及图像识别调用)

    上一篇实现了语音播放的功能,接下来就开始实现相机及图像识别功能调用. 首先,画一个大致的流程图方便理解: 以下是对相机/选择相册进行实现. (大家可以参考其它Android调用相机/选择相册的教程,不 ...

  8. android微信图片选择框架,Android仿微信图片选择器ImageSelector使用详解

    今天给大家介绍一个仿微信的图片选择器:ImageSelector.ImageSelector支持图片的单选.限数量的多选和不限数量的多选.支持图片预览和图片文件夹的切换.在上一篇文章 <Andr ...

  9. 按照日期排序相册库(支持自定义选中图片,视频数量,支持预览,支持拍摄仿小米原生相册)

    写在前面: 此库源于公司项目需求,暂时无法提供maven地址供大家使用,此处会给源码GitHub链接代码部分修改可直接运行. 库链接:https://github.com/XMDstar/TimeAl ...

最新文章

  1. CVPR2020 | 通过可微的代理投票损失进行6DoF对象位姿估计
  2. 失眠——耳部按摩(组图)
  3. VTK:图片之ImageMedian3D
  4. linux的网络不可达问题,我的服务器日志中的linux – (网络不可达)错误
  5. 【常见Web应用安全问题】
  6. go语言调用python_Golang中调用Python3
  7. 附一张css hack
  8. 数据结构小总结(成都磨子桥技工学校数据结构前12题)
  9. matlab如何把实验结果记录在文件中,实验一Matlab基本操作
  10. mysql 代码结构_MySQL代码执行结构
  11. FutureTask 源码解析
  12. 光伏电池matlab/simulink 搭建模型 可应用于光伏发电最大功率点跟踪 mppt中
  13. 片袖原型制图_袖原型一片袖打版干货
  14. python arma_Python实现ARMA模型
  15. Spring框架学习记录二:装配Bean
  16. RDA5856ETE系列_(1)新手入门
  17. 鼠标双击桌面上的快捷方式出现打开本快捷方式属性,而不是打开文件的可能原因及解决方法参考...
  18. 在VMware ESXi中使用固态硬盘
  19. cdn cfdn是什么_P2P+CDN=PCDN
  20. 谁动了我的奶酪之奶酪墙上的话。

热门文章

  1. 二维静电场有限元matlab,利用MATLAB计算电磁场有关分布
  2. linux内存与扇区,磁盘的基础知识——扇区、柱面、磁道、族
  3. 微信小程序中使用ECharts--折线图、柱状图、饼图等
  4. 基于标准库函数与基于HAL库函数的stm32编程方式对比
  5. MFC连接sqlite3数据库
  6. (C语言实现)页面置换——先进先出算法(FIFO)
  7. RedHat RHEL7.2  系统安装详细步骤
  8. 黑马JAVA P182 TCP通信快速入门
  9. 前端求职系列:如何写一份小程序简历(二)
  10. 求矩阵特征值和特征向量