系统 ios9.1
语言swift2.0
在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件
@IBAction func selectimageAction(sender: AnyObject) {}
这时候通常会d出来一个ActionSheet 上面有拍照 , 相册,录像 和取消 这几项。iOS 8 以后actionsheet 和 alertvIEw 都统一用UIAlertController 方法调用,8.3以前actionsheet 有独立的方法 后来都废弃了。首先我们要加入actionsheet 相关delegate 如下
class VIEwController: UIVIEwController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate {}
@IBAction func selectimageAction(sender: AnyObject) { let actionSheetController: UIAlertController = UIAlertController(Title: "请选择",message:nil,preferredStyle: .ActionSheet) //取消按钮 let cancelAction: UIAlertAction = UIAlertAction(Title: "取消",style: .Cancel) { action -> VoID in //Just dismiss the action sheet } actionSheetController.addAction(cancelAction) //拍照 let takePictureAction: UIAlertAction = UIAlertAction(Title: "拍照",style: .Default) { action -> VoID in [self .initWithImagePickVIEw("拍照")] } actionSheetController.addAction(takePictureAction) //相册选择 let choosePictureAction: UIAlertAction = UIAlertAction(Title: "相册",style: .Default) { action -> VoID in [self .initWithImagePickVIEw("相册")] } actionSheetController.addAction(choosePictureAction) //摄像 let movIEPictureAction: UIAlertAction = UIAlertAction(Title: "摄像",style: .Default) { action -> VoID in [self .initWithImagePickVIEw("摄像")] } actionSheetController.addAction(movIEPictureAction) self.presentVIEwController(actionSheetController,animated: true,completion: nil) }
这样我们想要的actionSheet 创建完成
仔细看上面的代码你会发现 [self .initWithImagePickVIEw(“拍照”)]
这个函数,我通过这个函数选择imagePicker 不同的参数
首先创建一个 var imagePicker : UIImagePickerController! 对象
然后实现相关方法
func initWithImagePickVIEw(type:Nsstring){ self.imagePicker = UIImagePickerController() self.imagePicker.delegate = self; self.imagePicker.allowsEditing = true; switch type{ case "拍照": self.imagePicker.sourceType = .Camera break case "相册": self.imagePicker.sourceType = .Photolibrary break case "录像": self.imagePicker.sourceType = .Camera self.imagePicker.vIDeoMaximumDuration = 60 * 3 self.imagePicker.vIDeoQuality = .Type640x480 self.imagePicker.mediaTypes = [String(kUTTypeMovIE)] break default: print("error") } presentVIEwController(self.imagePicker,completion: nil) }
swift 可以支持字符串,你也可以尝试其他类型看看!一些方法跟object c 基本一样
下面实现imagepick 的 delegate 方法就可以了
如下
对了如果要引入录制视频功能话别忘记了加
MobileCoreServices.framework 库 和 import MobileCoreServices 头文件
func imagePickerController(picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! String let compareResult = CFStringCompare(mediaType as Nsstring!,kUTTypeMovIE,CFStringCompareFlags.CompareCaseInsensitive) //判读是否是视频还是图片 if compareResult == CFComparisonResult.CompareEqualTo { let movIEPath = info[UIImagePickerControllerMediaURL] as? NSURL //获取路径 let movIEPathString = movIEPath!.relativePath if UIVIDeoAtPathIsCompatibleWithSavedPhotosAlbum(movIEPathString!) { UISaveVIDeoAtPathToSavedPhotosAlbum(movIEPathString!,nil,nil) } print("视频") } else { print("图片") let image = info[UIImagePickerControllerOriginalimage] as? UIImage self.imageVIEw.image = image; } imagePicker.dismissVIEwControllerAnimated(true,completion: nil) }
这样一个多媒体选择功能基本实现了!
总结以上是内存溢出为你收集整理的swift2.0 UIImagePickerController 拍照 相册 录像全部内容,希望文章能够帮你解决swift2.0 UIImagePickerController 拍照 相册 录像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)