標籤:
拍照
1 // MARK: - 拍照 2 func fromPhotograph() 3 { 4 if UIImagePickerController.isSourceTypeAvailable(.Camera) 5 { 6 //建立圖片控制器 7 let picker = UIImagePickerController() 8 9 //設定代理10 picker.delegate = self11 12 //設定來源13 picker.sourceType = UIImagePickerControllerSourceType.Camera14 15 16 //設定鏡頭17 if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)18 {19 picker.cameraDevice = UIImagePickerControllerCameraDevice.Front20 }21 22 //設定閃光燈23 picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On24 25 //允許編輯26 picker.allowsEditing = true;27 28 //開啟相機29 self.presentViewController(picker, animated: true, completion: { () -> Void in30 31 })32 33 }34 else35 {36 let aler = UIAlertView(title: "找不到相機!", message: nil, delegate: nil, cancelButtonTitle: "確定")37 aler.show()38 }39 }40 41 // MARK: - UIImagePickerControllerDelegate42 43 //選擇圖片成功之後代理44 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])45 {46 //查看info對象47 println(info)48 49 //擷取選擇的原圖50 // let image = info[UIImagePickerControllerOriginalImage] as UIImage51 52 //2015年5月2後修改53 let image = info[UIImagePickerControllerOriginalImage] as! UIImage54 55 //賦值,圖片視圖顯示圖片56 picView.image = image57 58 //圖片控制器退出59 picker.dismissViewControllerAnimated(true, completion: { () -> Void in60 61 })62 }63 64 //取消圖片控制器代理65 func imagePickerControllerDidCancel(picker: UIImagePickerController)66 {67 //圖片控制器退出68 picker.dismissViewControllerAnimated(true, completion: { () -> Void in69 70 })71 }72 73 74 // MARK: - UIActionSheetDelegate75 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)76 {77 if buttonIndex != actionSheet.cancelButtonIndex78 {79 if buttonIndex == 1 //從相簿選80 {81 self.fromAlbum()82 }else if buttonIndex == 2 //拍照83 {84 self.fromPhotograph()85 }86 }87 }
ios開發——實用技術篇Swift篇&拍照