简单实现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. Python 文件I/O (转)

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  2. Python序列的方法(转)

    在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表.表的元素可以更改,定值表一旦建立,其 ...

  3. 常用分组函数count-avg-sum-max-min

    分组函数也称多行函数,用于对一组数据进行运算,针对一组数据(取自于多行记录的相同字段)只返回一个结果,例如计算公司全体员工的工资总和.最高工资.最低工资.各部门的员工平均工资(按部门分组)等.由于分组 ...

  4. (转)Facade模式

    Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用. 就如同医院的接待员一样,Facade模式的Fac ...

  5. 东软实训4-JDBC连接数据库

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  6. Matlab 取子矩阵

    MATLAB如何提取矩阵的子块 在matlab中如何提取一个矩阵的部分元素 1.提取大矩阵的一列.一行元素: 一列元素:  A(:,j)表示提取A矩阵的第j列全部元素 一行元素:  A(i,:)表示提 ...

  7. C++程序设计教程学习(1)-第一部分 编程基础

    第一章 概述 C++到底难不难学?没有学不会的事情 1.1 程序设计语言 语言 编程语言 人和计算机交流的工具,群体扩大,人人间交流过程描述与信息表达的工具 机器语言,汇编语言,高级语言 1.2 C+ ...

  8. 贪心 CF 332 C 好题 赞

    题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...

  9. 求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字

    #include <iostream> using namespace std; int main() {long long s,n,i,j,p;s=0; cin>>n;    ...

  10. PHP的curl常用的5种写法

    // 1,抓取无访问控制文件 $ch= curl_init(); curl_setopt($ch, CURLOPT_URL,"http://localhost/mytest/phpinfo. ...