我正在尝试创建一个控制器来处理所有图像功能,以便您可以轻松地从任何视图控制器绑定所有相机操作。
理想情况下,我的目标是创建一个具有返回UIImage并允许我自己编写单个完成处理程序的函数的类
即。
let imagePicker = ImagePickerAlertController(frame:self.view.frame,controller:self)
imagePicker.displayAlert(){
imageValue in if let image = imageValue {
myImageView.image = image
}
}
然而,我似乎无法保存图像,甚至无法访问我从相机拍摄的图像。 imagePickerController函数似乎没有击中。
import UIKit
class ImagePickerAlertController: UIView, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var UIViewController : UIViewController?
let imagePicker: UIImagePickerController! = UIImagePickerController()
init(frame: CGRect, controller: UIViewController){
self.UIViewController = controller
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
self.UIViewController = nil
super.init(coder: aDecoder)
}
public func displayAlert(){
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let galleryAction = UIAlertAction(title: "Choose Photo",style:.default) {action -> Void in print("ok")}
let cameraAction = UIAlertAction(title: "Take Photo",style:.default) {action -> Void in self.takePicture() }
let cancelAction = UIAlertAction(title: "Cancel",style:.cancel) {action -> Void in }
alert.addAction(cancelAction)
alert.addAction(cameraAction)
alert.addAction(galleryAction)
self.UIViewController?.present(alert,animated:true,completion:nil)
}
private func takePicture() {
if (UIImagePickerController.isSourceTypeAvailable(.camera)){
if UIImagePickerController.availableCaptureModes(for: .rear) != nil || UIImagePickerController.availableCaptureModes(for: .front) != nil{
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
imagePicker.delegate = self
self.UIViewController?.present(imagePicker,animated: true,completion: nil)
}
else {
postAlert(title: "Rear camera doesn't exist",message:"Application cannot access the camera.")
}
}
else {
postAlert(title: "Camera inaccessable",message:"Application cannot access the camera.")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("got image")
if let pickedImage:UIImage = (info[UIImagePickerControllerOriginalImage]) as? UIImage {
let selectorToCall = Selector(("imageWasSavedSuccessfully:didFinishSavingWithError:context:"))
UIImageWriteToSavedPhotosAlbum(pickedImage, self, selectorToCall, nil)
}
imagePicker.dismiss(animated: true,completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("cancel")
self.UIViewController?.dismiss(animated: true, completion: nil)
}
func imageWasSavedSuccessfully(image: UIImage, didFinishSavingWithError error : NSError!, context: UnsafeMutablePointer<()>){
print("image saved")
if (error) != nil {
print(error)
}
else {
print("good to go")
}
}
func postAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.UIViewController?.present(alert, animated: true, completion: nil)
}
}
答案 0 :(得分:2)
问题是当imagePicker
已经有上面提到的模态视图控制器时,您尝试显示UIViewController
。
displayAlert():
self.UIViewController .present?(警报,动画:真,完成:无)
takePicture():
self.UIViewController?.present(imagePicker,animated:true,completion: 无)
所以你应该在你不需要的时候解雇UIAlertController:
let cameraAction = UIAlertAction(title: "Take Photo",style:.default) {action -> Void in
alert.dismiss(animated: true, completion: nil)
self.takePicture()
}
现在viewController可以present
没有任何问题