中和软件技术培训

上海中和软件公司培训专用博客

UIImagePickerController

UIImagePickerController示例


//
//  ViewController.swift
//  PhotoPicker
//
//  Created by Hu on 15/11/10.
//  Copyright © 2015年 Hu. All rights reserved.
//

import UIKit
import CoreMedia
import Photos
import MobileCoreServices

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate  {
    @IBOutlet weak var btnCamera: UIButton!
    @IBOutlet weak var btnPhotoLibrary: UIButton!


    @IBOutlet weak var btnSavedPhotoAlbum: UIButton!
    
    @IBOutlet weak var editSwitch: UISwitch!
    
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        print("Camera: \(UIImagePickerController.isSourceTypeAvailable(.camera))")
        print("photoLibrary: \(UIImagePickerController.isSourceTypeAvailable(.photoLibrary))")
        print("savedPhotosAlbum: \(UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum))")

        btnCamera.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
        btnPhotoLibrary.isEnabled = UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
        btnSavedPhotoAlbum.isEnabled = UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum)
    }
    
    @IBAction func CameraClick(_ sender: UIButton) {
        //判断设置是否支持图片库
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            //初始化图片控制器
            let picker = UIImagePickerController()
            //设置代理
            picker.delegate = self
            //指定图片控制器类型
            picker.sourceType = UIImagePickerControllerSourceType.camera
            //设置是否允许编辑
            picker.allowsEditing = editSwitch.isOn
            //弹出控制器,显示界面
            self.present(picker, animated: true, completion: {
                () -> Void in
            })
        }else{
            print("读取相册错误")
        }
        
    }
    
    @IBAction func PhotoLibrary(_ sender: UIButton) {
        //判断设置是否支持图片库
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            //初始化图片控制器
            let picker = UIImagePickerController()
            //设置代理
            picker.delegate = self
            //指定图片控制器类型
            picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            //设置是否允许编辑
            picker.allowsEditing = editSwitch.isOn
            //弹出控制器,显示界面
            self.present(picker, animated: true, completion: {
                () -> Void in
            })
        }else{
            print("读取相册错误")
        }

    }
    
    @IBAction func SavedPhotosAlbum(_ sender: UIButton) {
        //判断设置是否支持图片库
        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
            //初始化图片控制器
            let picker = UIImagePickerController()
            //设置代理
            picker.delegate = self
            //指定图片控制器类型
            picker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
            //设置是否允许编辑
            picker.allowsEditing = editSwitch.isOn
            //弹出控制器,显示界面
            self.present(picker, animated: true, completion: {
                () -> Void in
            })
        }else{
            print("读取相册错误")
        }
    }
    //选择图片成功后代理
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [String : Any]) {
        //查看info对象
        print(info)
        
        //显示的图片
        let image:UIImage!
        if editSwitch.isOn {
            //获取编辑后的图片
            image = info[UIImagePickerControllerEditedImage] as! UIImage
        }else{
            //获取选择的原图
            image = info[UIImagePickerControllerOriginalImage] as! UIImage
        }
        
        imageView.image = image
        //图片控制器退出
        picker.dismiss(animated: true, completion: {
            () -> Void in
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

博文最后更新时间:


评论

  • 胡老师 管理员

    注意:Camera的时候,需要在info.plist中设置Camera Usage Description 使用相机时候向客户申请权限时候的文字

发表评论

博客统计

访问量:12710

博文总数:13 评论总数:6

原创12 翻译0 转载1