简单实现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)

}

}

swift调用相机和相册的更多相关文章

  1. 转:HTML5页面如何在手机端浏览器调用相机、相册功能

    HTML5页面如何在手机端浏览器调用相机.相册功能 开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file&q ...

  2. HTML5页面如何在手机端浏览器调用相机、相册功能

    最近在做一个公司的保险信息处理系统项目,开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file"/& ...

  3. 一、H5(移动端)前端使用input type=file 上传图片,调用相机和相册

    一.H5(移动端)前端使用input type=file 上传图片,调用相机和相册

  4. H5调用相机和相册更换头像

    需求:H5调用手机的相机和相册从而实现更换头像的功能 这个功能是很常用的一个功能,因此做一个记录. 1.在头像img下加一个文件输入框 <input type="file" ...

  5. Android 调用相机、相册功能

    清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述) private static final int REQUEST_CODE_ALBUM = 100 ...

  6. Android开发在Activity外申请权限调用相机打开相册

    问题描述: 最近在项目中遇到一个需要调用相册和打开相机的需求,但是,在Android 6.0以后,调用相册属于危险权限,需要开发者动态获取,这就意味着我们申请权限是与Activity绑定的,但如果一个 ...

  7. iOS9中,swift判断相机,相册权限,选取图片为头像

    在iOS7以后要打开手机摄像头或者相册的话都需要权限,在iOS9中更是更新了相册相关api的调用 首先新建一个swift工程,在SB中放上一个按钮,并在viewController中拖出点击事件 ok ...

  8. IOS调用相机和相册时无法显示中文

    调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文 需要在info.plist做如下设置 info.plist里面添加 Localizedresourc ...

  9. iOS通过UIAlertController弹出底部选择框来调用相机或者相册

    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredSt ...

随机推荐

  1. html5.边框属性相关知识点

    border-left 定义左边框 border-top 定义上边框 border-right 定义有边框 border-bottom 定义下边框 边框样式: dotted 边框线为点状虚线 dash ...

  2. 反射那些事儿——Java动态装载和反射技术

    一直以来反射都是只闻其声,却无法将之使用,近日尽心下来学习下,发现了很多精妙之处. Java动态装载和反射技术 一.类的动态装载 1.Java代码编译和执行的整个过程包含了以下三个重要的机制: ● J ...

  3. FileGeodatabase和PersonalGeodatabase与ArcSDEGeodatabase三种数据库比较.

  4. JCo 指南

    http://blog.csdn.net/asdfak/article/details/5834731 JAVA 调用SAP端接口 Java Connector and BAPI 前些日子想去深入的研 ...

  5. java开发软件的安装

    jdk+eclipse+svn+maven+mysql+tomcat7.0+sublime安装包和jar插件 配置管理工具-SVN http://download.csdn.net/detail/u0 ...

  6. 网络流——增广路算法(dinic)模板 [BeiJing2006]狼抓兔子

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  7. linux一些常用指令整理

    set number:设置行号 set list:区分tab和空格 按w:一个字一个字跳转 按b:一个字一个字回跳 shift+6:行首 shift+4:行尾 ctrl+v:选中块,再按shift+i ...

  8. edit编辑框相关

    从Edit Control获取值,然后通过MessageBox输出出来 void CNowaMagic_MFCDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代 ...

  9. 在.NET MVC下不用iframe实现局部加载html

    最近在做个后台系统,之前都是用iframe来实现加载内容,左侧菜单不刷新.但一直不喜欢这种方法,有许多弊端.今天自己在网上查找了一番后找到了比较好的替代方案: 一.利用html的锚点标记来实现无刷新页 ...

  10. FileZilla Server下载以及安装使用

    新版本filezilla server已经不能在windows xp和windows20003下使用了 下面是可以在xp和2003下使用的最后版本下载地址 http://pan.baidu.com/s ...