多媒体应用-swift
照片选择主要是通过UIImagePickerController控制器实例化一个对象,然后通过self.PresentViewController方法推出界面显示。需要实现代理UIImagePickerControllerDelegate,UINavigationControllerDelegate。
通过isSourceTypeAvailable方法判断设置是否支持照相机、图片库、相册功能。
使用相册功能主要以下一个步骤:
1)判断是否支持要使用的图片库或相机功能;
2)初始化图片控制器对象;
3)指定图片控制器对象的代理;
4)指定图片控制器类型;
5)弹出显示图片控制器;
6)实现图片控制器代理方法。
// MARK: - 选择照片
/*----- 选择照片 ------*/
@IBAction func addImageButtonClick()
{
let actionSheet = UIActionSheet(title: "请选择", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从相册选","拍照")
actionSheet.showInView(self.view)
}
// MARK: - UIActionSheetDelegate
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex != actionSheet.cancelButtonIndex
{
if buttonIndex == //从相册选
{
self.fromAlbum()
}else if buttonIndex == //拍照
{
self.fromPhotograph()
}
}
}
// MARK: - 选取相册
func fromAlbum()
{
//判断设置是否支持图片库
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)
{
//初始化图片控制器
let picker = UIImagePickerController() //设置代理
picker.delegate = self //设置媒体类型
picker.mediaTypes = [kUTTypeImage as String,kUTTypeVideo as String] //设置允许编辑
picker.allowsEditing = true //指定图片控制器类型
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary //弹出控制器,显示界面
self.presentViewController(picker, animated: true, completion: { () -> Void in }) }
else
{
let aler = UIAlertView(title: "读取相册错误!", message: nil, delegate: nil, cancelButtonTitle: "确定")
aler.show()
}
}
// MARK: - 拍照
func fromPhotograph()
{
if UIImagePickerController.isSourceTypeAvailable(.Camera)
{
//创建图片控制器
let picker = UIImagePickerController() //设置代理
picker.delegate = self //设置来源
picker.sourceType = UIImagePickerControllerSourceType.Camera //设置镜头
if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)
{
picker.cameraDevice = UIImagePickerControllerCameraDevice.Front
} //设置闪光灯
picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On //允许编辑
picker.allowsEditing = true; //打开相机
self.presentViewController(picker, animated: true, completion: { () -> Void in }) }
else
{
let aler = UIAlertView(title: "找不到相机!", message: nil, delegate: nil, cancelButtonTitle: "确定")
aler.show()
}
}
// MARK: - UIImagePickerControllerDelegate
//选择图片成功之后代理
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//查看info对象
print(info)
//获取选择的原图
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
//赋值,图片视图显示图片
picView.image = image
//图片控制器退出
picker.dismissViewControllerAnimated(true, completion: { () -> Void in
})
}
//取消图片控制器代理
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
//图片控制器退出
picker.dismissViewControllerAnimated(true, completion: { () -> Void in
})
}
可以通过引入MobileCoreServices.framework,来设置mediaTypes属性设置媒体的类型。
多媒体应用-swift的更多相关文章
- 关东升的《iOS实战:图形图像、动画和多媒体卷(Swift版)》上市了
关东升的<iOS实战:图形图像.动画和多媒体卷(Swift版)>上市了 承蒙广大读者的厚爱我的<iOS实战:图形图像.动画和多媒体卷(Swift版)>京东上市了,欢迎广大读者提 ...
- iOS开发——UI篇Swift篇&玩转UItableView(一)基本使用
UItableView基本使用 class ListViewController: UIViewController , UITableViewDataSource, UITableViewDeleg ...
- OC 和 swift 小结
1 什么是 OC 语言? OC 语言即面向对象语言,它扩展了 ANSI C 语言,将 SmallTalk 式的消息传递机制加入到 ANSI C 中.它是苹果 OS 和 iOS 以及相关的 API,Co ...
- iOS开发——实用篇Swift篇&项目开发常用实用技术
项目开发常用实用技术 实现拨打电话 要实现打电话功能,最简单最直接的方式便是:直接跳到拨号界面 (注意:这个需要真机调试,模拟器无效果) UIApplication.sharedApplica ...
- Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)
游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...
- Swift 3 :基于 AVAudioPlayer 的简单音乐播放器
2017.05.22 17:46* 字数 1585 阅读 5095评论 0喜欢 8赞赏 2 https://www.jianshu.com/p/4d5c257428a1 学习ios以来差不多接近两个月 ...
- iOS代码规范(OC和Swift)
下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...
- Swift与C#的基础语法比较
背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
随机推荐
- Java-Swing嵌入浏览器(一)
今天要说的额是浏览器的第一个版本是用DJnative-swt和swt包开发的调用本地浏览器和webkit浏览器的示例 这是我的工程目录[源码见最后]: src下为写的源码,lib为引入的swt和DJn ...
- IPAddress
Console.WriteLine("BitConverter.IsLittleEndian = {0}", BitConverter.IsLittleEndian); Conso ...
- Running an etcd cluster on localhost
Purpose Run a cluster on localhost while investigating etcd Use a static cluster (So we have no exte ...
- 结构体dfield_t
/* SQL data field struct */ typedef struct dfield_struct dfield_t; /** Structure for an SQL data fie ...
- Native Fullscreen JavaScript API (plus jQuery plugin)
http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/ HTML5 <video> is gre ...
- C# String.Format大全
C# String.Format大全 ? ? ? 十进制的数字 ? ? string.Format("{0:D3}",23) 023 格式化十进制的数字 string.Format ...
- Android Support v4、v7、v13的区别和应用场景
N久未做android了,以前做的时候,2.2才刚出来,现在android都更新到了4.3了,而从前一段时间android各个sdk版本市场占有率 来看,1.6.2.1还是占有一定的市场,故在有些时候 ...
- 【转】Compile FFmpeg on CentOS 6.x
This guide is based on a minimal CentOS installation and will install FFmpeg with several external e ...
- php升级到5.4
这里使用 Webtatic EL6的YUM源来安装php5.4,我们首页安装Webtatic EL6 YUM源 rpm -Uvh http://repo.webtatic.com/yum/el6/la ...
- console.read()读入的内容
今天写的特别简单的代码,大体是一个模式选择,从控制台读入一个数,然后做出相应的选择. 代码如下: using System; using System.Collections.Generic; usi ...