iOS 选取用户相册中的图片 UIImagePickerController
[GitHub] iOS Demo : https://github.com/KyleBing/iosDemo
该文中的例子可以从上面这个 github 项目中找到,包含许多其它常用UI
组件的使用
选取用户相册中图片,或者拍照的方式通过 UIImagePickerController
来实现
内容
从相册中选取一张图片,并按比例显示在当前 ViewController
的中间位置
步骤
VC
=ViewController
方便说明,所以简写
- 在当前
VC
中 实现UIImagePickerControllerDelegate
、UINavigationController
这两个协议 - 新建一个
picker: UIImagePickerController
和imageView: UIImageView
- 设置这个 picker 的
delegate
是 当前VC
- 设置
imageView
的内容展示属性:比例缩放 - 设置选取照片的触发方法,比如在
NavigationBar
中添加一个按钮 -
实现 UIImagePickerControllerDelegate 的两个方法
- 用户选中一张图片时:获取 info 中的信息并进行相关操作,最后使选择窗口消失
- 用户取消选择时,使选择窗口消失
效果
代码及注释
//
// ImagePickerVC.swift
// iosDemo
//
// Created by Kyle on 2020/3/16.
// Copyright © 2020 KyleBing. All rights reserved.
//
import UIKit
/// 0. 这个 ViewController 需要实现 UIImagePickerControllerDelegate 和 UINavigationControllerDelegate 两个协议
class ImagePickerVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
/// 1. 新建 UIImagePickerController 对象
var imagePicker: UIImagePickerController = UIImagePickerController()
lazy var imageView = UIImageView(frame: view.frame)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Pick", style: .plain, target: self, action: #selector(pickAnImage))
/// 2. 设置 picker 的 delegate 和 相关设置
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
/// 3. 设置 imageView 的属性
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
}
@objc func pickAnImage(){
/// 4. 点击选图片时,展示这个 picker controller
present(imagePicker, animated: true) {
print("UIImagePickerController: presented")
}
}
// MARK: - Image picker delegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
/// 5. 用户选中一张图片时触发这个方法,返回关于选中图片的 info
/// 6. 获取这张图片中的 originImage 属性,就是图片本身
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
fatalError("error: did not picked a photo")
}
/// 7. 根据需要做其它相关操作,这里选中图片以后关闭 picker controller 即可
picker.dismiss(animated: true) { [unowned self] in
// add a image view on self.view
self.imageView.image = selectedImage
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
/// 8. 用户点击取消时
picker.dismiss(animated: true) {
print("UIImagePickerController: dismissed")
}
}
}
info 内容
__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.JPG?id=557741A9-A05A-468E-AFAB-9B759CB66660&ext=JPG
__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerOriginalImage): <UIImage:0x283e360a0 anonymous {2048, 1536}>
__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerMediaType): public.image
__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerImageURL): file:///private/var/mobile/Containers/Data/Application/A5875FF6-3D3C-4147-BF5B-AD850397694D/tmp/CC744A41-B42F-490E-9B5F-5AF06CDD24C4.jpeg]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。