ios开发——实用技术篇Swift篇&拍照

拍照

     // 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: [NSObject : AnyObject])
     {
         //查看info对象
         println(info)

         //获取选择的原图
 //        let image = info[UIImagePickerControllerOriginalImage] as UIImage

         //2015年5月2后修改
         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

         })
     }

     // MARK: - UIActionSheetDelegate
     func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
     {
         if buttonIndex != actionSheet.cancelButtonIndex
         {
               //从相册选
             {
                 self.fromAlbum()
             } //拍照
             {
                 self.fromPhotograph()
             }
         }
     }
 
上一篇:ios开发——实用技术篇Swift篇&地址薄、短信、邮件


下一篇:ios开发——实用技术篇Swift篇&照片选择