关闭

拍照的一些应用

124人阅读 评论(0) 收藏 举报
分类:

从相册中获取一些照片,可以直接打开模拟器的相册,随后拖入一张照片即可

从相册中获取照片 并显示出来 可以分为以下几步

1 判断是否支持要使用的图片库或相册功能

2 初始化图片控制器对象

3指定图片控制器的代理

4指定图片控制器的类型

5弹出图片控制器

6实现图片控制器代理方法

在sb拖进一个Button 为start 

随后代码如下

import UIKit
//实现照片的代理
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    override func viewDidLoad() {
    super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }
         override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
            }
    @IBAction func change(sender: UIButton) {
        //判断设置是否支持图片库
        if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
            //初始化图片控制器
            let picker = UIImagePickerController()
            //设置代理
            picker.delegate = self
            //指定图片控制器的类型
            picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            //弹出控制器 显示界面
            self.presentViewController(picker, animated: true, completion: nil )
        }else {
            let alert = UIAlertController(title: "读取错误", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            //提醒框的tanchu
            self.presentViewController(alert, animated: true, completion: nil)
            
        }
    }
    //选择图片成功后代理
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print(info)
    //获取选择的图片
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        //赋值图片显示
        var imageView = UIImageView(image: image)
        self.view.addSubview(imageView)
        picker.dismissViewControllerAnimated(true, completion: nil )
    }
    //取消图片控制器代理
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        //图片控制器的推出
        picker.dismissViewControllerAnimated(true, completion: nil )
    }
    }

拍照功能也和上述的差不多

import UIKit
//实现照片的代理
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    //初始化图片控制器
let picker = UIImagePickerController()
    override func viewDidLoad() {
    super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }
         override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
            }
    @IBAction func change(sender: UIButton) {
        //判断设置是否支持图片库
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {
           
            //设置代理
            picker.delegate = self
            //指定图片控制器的类型
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            //允许编辑
            picker.allowsEditing = true
            //打开相机
            self.presentViewController(picker, animated: true, completion: nil )
        }else {
            let alert = UIAlertController(title: "找不到相机", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            //提醒框的tanchu
            self.presentViewController(alert, animated: true, completion: nil)
            
        }
  //设置前置的镜头
        if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front) {
         picker.cameraDevice = UIImagePickerControllerCameraDevice.Front
        }
        //设置闪光灯
        picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On
    }
       }
因为模拟器没有相机所以会线程崩溃



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66988次
    • 积分:2554
    • 等级:
    • 排名:第14443名
    • 原创:197篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    最新评论