使用Swift 3,這裏是我的。我的代碼有(1)選擇視圖控制器,它使用UIImagePickerController來使用相機或從相機膠捲中選擇,然後(2)續集到編輯視圖控制器,我將按鈕的代碼除去,如我沒有使用IB。
class SelectViewController: UIViewController {
// selection and pass to editor
let picker = ImagePickerController()
var image = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
}
}
}
}
如果不segueing另一個VC,除去.performSegue通話和下面的最終成績代碼:符號。 (相機/選定的圖像在圖片變種)
來源
2017-01-19 21:10:11
dfd
您是否使視圖代表?你有** imagePicker.delegate = self ** someplace(可能viewDidLoad)? – dfd
第一種方法@dfd – Steve
對不起,錯過了。我看到的唯一的另一件事是這是你的第二個覆蓋。你在使用Swift 3嗎?如果是這樣,它是** func imagePickerController(_ picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:Any]){** – dfd