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

拍照

 

 1     // MARK: - 拍照

 2     func fromPhotograph()

 3     {

 4         if UIImagePickerController.isSourceTypeAvailable(.Camera)

 5         {

 6             //创建图片控制器

 7             let picker = UIImagePickerController()

 8             

 9             //设置代理

10             picker.delegate = self

11             

12             //设置来源

13             picker.sourceType = UIImagePickerControllerSourceType.Camera

14             

15             

16             //设置镜头

17             if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)

18             {

19                 picker.cameraDevice = UIImagePickerControllerCameraDevice.Front

20             }

21             

22             //设置闪光灯

23             picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On

24             

25             //允许编辑

26             picker.allowsEditing = true;

27             

28             //打开相机

29             self.presentViewController(picker, animated: true, completion: { () -> Void in

30                 

31             })

32             

33         }

34         else

35         {

36             let aler = UIAlertView(title: "找不到相机!", message: nil, delegate: nil, cancelButtonTitle: "确定")

37             aler.show()

38         }

39     }

40     

41     // MARK: - UIImagePickerControllerDelegate

42     

43     //选择图片成功之后代理

44     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])

45     {

46         //查看info对象

47         println(info)

48         

49         //获取选择的原图

50 //        let image = info[UIImagePickerControllerOriginalImage] as UIImage

51         

52         //2015年5月2后修改

53         let image = info[UIImagePickerControllerOriginalImage] as! UIImage

54         

55         //赋值,图片视图显示图片

56         picView.image = image

57         

58         //图片控制器退出

59         picker.dismissViewControllerAnimated(true, completion: { () -> Void in

60             

61         })

62     }

63     

64     //取消图片控制器代理

65     func imagePickerControllerDidCancel(picker: UIImagePickerController)

66     {

67         //图片控制器退出

68         picker.dismissViewControllerAnimated(true, completion: { () -> Void in

69             

70         })

71     }

72     

73     

74     // MARK: - UIActionSheetDelegate

75     func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)

76     {

77         if buttonIndex != actionSheet.cancelButtonIndex

78         {

79             if buttonIndex == 1  //从相册选

80             {

81                 self.fromAlbum()

82             }else if buttonIndex == 2 //拍照

83             {

84                 self.fromPhotograph()

85             }

86         }

87     }

 

 

 

你可能感兴趣的:(swift)