打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
swift2.0 UIImagePickerController 拍照 相册 录像

系统 ios9.1
语言swift2.0

在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件

@IBAction func selectImageAction(sender: AnyObject) {}
  • 1
  • 2
  • 3

这时候通常会弹出来一个ActionSheet 上面有拍照 , 相册,录像 和取消 这几项。iOS 8 以后actionsheet 和 alertview 都统一用UIAlertController 方法调用,8.3以前actionsheet 有独立的方法 后来都废弃了。首先我们要加入actionsheet 相关delegate 如下

class ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate {}
  • 1
  • 2
  • 3
     @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)    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

这样我们想要的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, animated: true, completion: nil)    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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, nil)            }            print("视频")        }        else {            print("图片")        let image = info[UIImagePickerControllerOriginalImage] as? UIImage        self.imageView.image =  image;        }        imagePicker.dismissViewControllerAnimated(true, completion: nil)    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

这样一个多媒体选择功能基本实现了!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
swift 从相册里取图片并Post传到服务器上
Tesseract OCR(光学字符识别)教程
Swift-一步步教你上传头像
iPad与iPhone调用UIImagePickerViewController方法
UIActionSheet的使用和系统相机的相关调用
IOS开发之相册拾取器UIImagePickerController
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服