加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift学习笔记(4)使用UIImagePickerController实现从设备图片

发布时间:2020-12-14 06:56:27 所属栏目:百科 来源:网络整理
导读:Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片 设备图片库和照相机是图像的两个重要来源,使用UIKit中提供的图像选择器UIImagePickerController可以轻易地实现从设备图片库和照相机获取图片。 目录 Swift学习笔记4使用UII

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

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

目录

  • Swift学习笔记4使用UIImagePickerController实现从设备图片库和照相机获取图片
    • 目录
    • 声明协议
    • 创建UIImagePickerController
    • UIImagePickerControllerDelegate委托
    • UINavigationControllerDelegate协议
    • 图片编辑
    • iOS 9 中的新错误

声明协议

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,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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读