【发布时间】:2016-12-20 20:13:48
【问题描述】:
我想在我的应用程序中添加一个摄像头界面,具有捕获和保存图像功能,但是当我运行此应用程序时我无法获得任何东西,构建成功且没有任何错误,但 viewController 上没有显示任何内容。
class ViewController: UIViewController , UINavigationControllerDelegate, UIImagePickerControllerDelegate{
@IBOutlet var imageView: UIImageView!
@IBOutlet var TakePhoto: UIButton!
var imagePick: UIImagePickerController!
var newMedia: Bool?
@IBAction func TakePhoto(_ sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePick = UIImagePickerController()
imagePick.delegate = self
imagePick.sourceType = UIImagePickerControllerSourceType.camera
imagePick.mediaTypes = [kUTTypeImage as String]
imagePick.allowsEditing = false
self.present(imagePick, animated: true, completion: nil)
newMedia = true
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as String){
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
if(newMedia == true){
UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
} //else if mediaType.isEqual(to: kUTTypeMovie as String)
self.dismiss(animated: true, completion: nil)
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer){
if error != nil{
let alert = UIAlertController(title: "Save Failed", message: "Failed to save the image", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
}
【问题讨论】:
-
您的 IBOutlet 连接正确吗?
-
是的,他们关系很好。 ??????
标签: ios swift swift3 ios-camera