简单实现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. 如何使用easyUI

    一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...

  2. 浏览器格式化JSON输出,thinkphp

    1 //编写类方法用$this->ajaxReturn()返回数据 2 public function index(){ 3 $user = M('User'); 5 $data = $user ...

  3. NUnit单元测试初试

    创建项目,创建几个方法 创建测试类 开启NUnit测试工具,新建一个测试项目 打开测试的程序集 选择节点,点击测试,绿色通过,红色说明有错误

  4. HTML5 Canvas图像放大、移动实例1

    1.前台代码 <canvas id="canvasOne" class="myCanvas" width="500" height=& ...

  5. SqlServer 不同服务器之间数据库连接、数据库登录、数据传递

    需求:我是本地数据库想纯SQL访问其它服务器上的数据库,而不使用数据库客户端的连接.这里面就想到了数据库link,通过下面的代码进行创建以后,就可以在本地对链接的服务器数据库进行操作了--添加SQLS ...

  6. C#中的委托是什么?

    概述 委托类似C++中的函数指针,但是又有所不同.在C++中,函数指针不是类型安全的,它指向的是内存中的某一个位置,我们无法判断这个指针实际指向什么,对于参数和返回类型就更难以知晓.而.NET的委托则 ...

  7. C# 前台线程与后台线程区别

    using System; using System.Drawing; using System.Windows.Forms; using System.Threading; namespace Wi ...

  8. 《图解CSS3》——笔记(二)

    作者:大漠 勘误:http://www.w3cplus.com/book-comment.html 2014年7月15日15:58:11 第二章  CSS3选择器 2.1  认识CSS选择器 2.1. ...

  9. python生成html文件浏览器中文显示乱码问题

    近来在网上采集数据,想把采集下来的数据整合成html的形式保存.以便其他的平台产品可以直接读取html显示或者根据html标签提取数据. def output_html(self): try: fou ...

  10. 关于js事件冒泡和时间捕获

    (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...