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

简介: Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片设备图片库和照相机是图像的两个重要来源,使用UIKit中提供的图像选择器UIImagePickerController可以轻易地实现从设备图片库和照相机获取图片。目录Swift学习笔记4使用UIImagePickerController实现从设备

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

设备图片库和照相机是图像的两个重要来源,使用UIKit中提供的图像选择器UIImagePickerController可以轻易地实现从设备图片库和照相机获取图片。

目录

声明协议

UIViewController需声明实现如下两个协议

class viewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate{
...
}

创建UIImagePickerController

定义一个UIImagePickerController

var imagePicker:UIImagePickerController!

创建一个UIButton,在其IBAction中添加代码

设备图片库:

if self.imagePicker == nil{
    self.imagePicker = UIImagePickerController()
}
self.imagePicker.delegate = self
//设置图片来源为设备图片库
self.imagePicker.sourceType = .PhotoLibrary
self.presentViewController(self.imagePicker, animated: true, completion: nil)

照相机:

if UIImagePickerController.isSourceTypeAvailable(.Camera){
   if self.imagePicker == nil{
       self.imagePicker = UIImagePickerController()
   }
   self.imagePicker.delegate = self
   //设置图片来源为相机
   self.imagePicker.sourceType = .Camera
   self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
 else{
  //弹出警告框
  let errorAlert = UIAlertController(title: "相机不可用", message: "", preferredStyle: UIAlertControllerStyle.Alert)
  let cancelAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Cancel, handler: nil)
  errorAlert.addAction(cancelAction)
  self.presentViewController(errorAlert, animated: true, completion: nil)
 }

UIImagePickerControllerDelegate委托

取消图片获取:

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
     self.imagePicker = nil
     self.dismissViewControllerAnimated(true, completion: nil)
}

完成图片获取:


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
     //从info中取出获取的原始图片
     let image = info[UIImagePickerControllerOriginalImage] as! UIImage    
     self.imageView.image = image
     //设置图片显示模式
     self.imageView.contentMode = .ScaleAspectFill
     self.imagePicker.delegate = nil
     self.dismissViewControllerAnimated(true, completion: nil)
}

UINavigationControllerDelegate协议

以下两个协议可以根据需求来选择是否实现

- navigationController:willShowViewController:animated
- navigationController:didShowViewController:animated

图片编辑

如果要将原始图片进行编辑如缩放,裁剪等后再使用

则在创建UIImagePickerController时添加:

self.imagePicker.allowsEditing = true

然后将实现UIImagePickerControllerDelegate中的

let image = info[UIImagePickerControllerOriginalImage] as! UIImage 

改为

let image = info[UIImagePickerControllerEditedImage] as! UIImage

iOS 9 中的新错误

如果在iOS 9 Xcode 7.1 以上的版本运行可能会报以下错误

_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15)

解决方法:

打开Info.plist,将Localization native development region中的值由en改为United States

目录
相关文章
|
存储 Swift
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
Swift - Cell自适应+代码约束(SnapKit)横竖屏支持平铺+根据URL获取图片size
179 0
|
Swift
Swift - 根据图片URL获取图片的大小
Swift - 根据图片URL获取图片的大小
470 0
|
Swift
Swift学习笔记——页面跳转
创建一个single view app后,项目中有main.storyboard,里面是一个viewcontroller。 那么我们如何实现页面跳转 首先添加一个导航控制器Navigation Controller。点击右上的➕,然后选择控件拖到面板上即可
1150 0
|
前端开发 测试技术 Swift
Swift学习笔记——新建项目
在xcode菜单中选择 new -> project -> single view app 点击next在弹出窗中填写项目名称 这里languge有可以选择object-c或swift作为项目语言。这里我们学习swift,所以选择swift。 如果language选择swift,下面的user interface可以选择swiftUI和storyboard。 SwiftUI是2019 年 WWDC 大会上,苹果在压轴环节向大众宣布了基于 Swift 语言构建的全新 UI 框架,与flutter类似,是用代码编写页面,支持快速预览。
664 0

相关课程

更多