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

配置:

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

从相册选择图片:

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

import UIKit

class 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.photoLibrary
            self.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? UIImage
        self.imageView.image = image
        
        picker.dismiss(animated: true, completion: nil)
    }

}

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

从相机选择图片:

import UIKit

class 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 = self
            pickerVC.sourceType = UIImagePickerController.SourceType.camera
            self.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? UIImage
        self.imageView.image = image
        
        picker.dismiss(animated: true, completion: nil)
    }

}

闪光灯开启:

pickerVC.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On

使用后置相机:

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

效果:

Swift使用UIImagePickerController 从相册选择图片、从相机选择图片_第2张图片
Swift使用UIImagePickerController 从相册选择图片、从相机选择图片_第3张图片

你可能感兴趣的:(iOS,ios,swift,xcode)