zoukankan      html  css  js  c++  java
  • swift调用相机和相册

    简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步

    import UIKit

     

    class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

     

        var imgView = UIImageView()

     

        var img = UIImage()

     

        override func viewDidLoad() {

     

            super.viewDidLoad()

     

            // Do any additional setup after loading the view, typically from a nib.

     

            var btn = UIButton()

     

            btn.frame = CGRectMake(50, 120, 200, 40)

     

            btn.backgroundColor = UIColor.orangeColor()

     

            btn.addTarget(self, action: "goCamera", forControlEvents: UIControlEvents.TouchUpInside)

     

            self.view.addSubview(btn)

     

            

     

            var btn1 = UIButton()

     

            btn1.frame = CGRectMake(50, 200, 200, 40)

     

            btn1.backgroundColor = UIColor.redColor()

     

            btn1.addTarget(self, action: "goImage", forControlEvents: UIControlEvents.TouchUpInside)

     

            self.view.addSubview(btn1)

     

            

     

            imgView.frame = CGRectMake(100, 260, 100, 100)

     

            self.view.addSubview(imgView)

     

        }

     

     

     

        override func didReceiveMemoryWarning() {

     

            super.didReceiveMemoryWarning()

     

            // Dispose of any resources that can be recreated.

     

        }

     

        

     

        //打开相机

     

        func goCamera(){

     

            //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库

     

            var sourceType = UIImagePickerControllerSourceType.Camera

     

            if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

     

                sourceType = UIImagePickerControllerSourceType.PhotoLibrary

     

            }

     

            var picker = UIImagePickerController()

     

            picker.delegate = self

     

            picker.allowsEditing = true//设置可编辑

     

            picker.sourceType = sourceType

     

            self.presentViewController(picker, animated: true, completion: nil)//进入照相界面

     

        }

     

        

     

        func goImage(){

     

            var pickerImage = UIImagePickerController()

     

            if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){

     

                pickerImage.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

     

                pickerImage.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(pickerImage.sourceType)!

     

            }

     

            pickerImage.delegate = self

     

            pickerImage.allowsEditing = true

     

            self.presentViewController(pickerImage, animated: true, completion: nil)

     

        }

     

        //选择好照片后choose后执行的方法

     

        func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){

     

            println("choose--------->>")

     

            println(info)

     

            img = info[UIImagePickerControllerEditedImage] as UIImage

     

            imgView.image = img

     

            picker.dismissViewControllerAnimated(true, completion: nil)

     

        }

     

        //cancel后执行的方法

     

        func imagePickerControllerDidCancel(picker: UIImagePickerController){

     

            println("cancel--------->>")

     

            picker.dismissViewControllerAnimated(true, completion: nil)

     

        }

     

    }

  • 相关阅读:
    基于nginx的rtmp的服务器(nginx-rtmp-module)
    基于nginx的HLS简单服务器搭建
    HLS(HTTP Live Streaming)协议之m3u8文件生成方式
    基于live555的一个简单RTSP服务器
    基于webrtc的多人视频会话的demo运行程序
    写给小白的Python之019:面向对象-类方法、静态方法
    写给小白的Python之018:面向对象-私有成员、@property
    写给小白的Python之017:面向对象-封装、继承、多态
    写给小白的Python之016:面向对象-魔法方法
    写给小白的Python之015:面向对象-类和对象
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5150403.html
Copyright © 2011-2022 走看看