简单实现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. Node.js 实现简单小说爬虫

    最近因为剧荒,老大追了爱奇艺的一部网剧,由丁墨的同名小说<美人为馅>改编,目前已经放出两季,虽然整部剧槽点满满,但是老大看得不亦乐乎,并且在看完第二季之后跟我要小说资源,直接要奔原著去看结 ...

  2. Intellj IDEA 启动参数调优

    (修改前记得备份) 修改IntellJ/bin/idea.exe.vmoptions修改成 -Xms512m -Xmx512m -Xmn164m -XX:MaxPermSize=250m -XX:Re ...

  3. javascript封装id|class|元素选择器

    由于各个浏览器都支持的选择方法只有如下三种: 1 document.getElementById() 2 document.getElementsByName() 3 document.getElem ...

  4. 关于ubuntu中的软件安装

    在ubuntu中一般使用apt-get来安装软件工具, 例如 sudo apt-get install g++ apt-get会在镜像库中找到你需要的软件镜像(例如 g++)来安装,那么apt-get ...

  5. asp.net读取Excel中的数据问题

      1.Microsoft.ACE.OLEDB.12.0 与Microsoft.Jet.OLEDB.4.0 在通过ADO对Excel对象进行连接时(此时Excel则认为是一个数据源),需要配置对Exc ...

  6. SQL Server 中创建数据库、更改主文件组示例

    以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...

  7. HTML 学习网站

    http://www.w3school.com.cn/tiy/t.asp?f=html_intro

  8. MySQL 忘记密码后的重置操作

    一.修改配置文件方式        1.关闭 MySQL                 linux:                        1)service mysqld stop     ...

  9. json_encode如何防止汉字转义成unicode

    众所周知,json_encode通常会把json中的汉字转义成unicode,但是这并不一定是我们想要的.有时候,我们需要获得汉字形式的json字符串,比如需要获得gbk编码的json字符串(只要把汉 ...

  10. POJ1961 KMP算法

    POJ1961 问题重述: 输入一个长度为l的字符串S,求所有S的由字串重复排列而成的前缀,并输出前缀的长度以及该前缀的最大重复度. AC代码: //Memory: 5700K Time: 641MS ...