gpt4 book ai didi

ios - UIImagePickerControllerDelegate 未被调用

转载 作者:行者123 更新时间:2023-11-28 12:38:47 25 4
gpt4 key购买 nike

我正在尝试创建一个 Controller 来处理所有图像功能,以便您可以轻松地从任何 View Controller 绑定(bind)所有相机操作。

理想情况下,我的目标是创建一个具有返回 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)
}

}

最佳答案

问题在于,当您的 UIViewController 已经具有上面显示的模态视图 Controller 时,您尝试呈现 imagePicker

displayAlert():

self.UIViewController?.present(alert,animated:true,completion:nil)

拍照():

self.UIViewController?.present(imagePicker,animated: true,completion: nil)

因此,您应该在不需要时立即关闭 UIAlertController:

let cameraAction = UIAlertAction(title: "Take Photo",style:.default) {action -> Void in
alert.dismiss(animated: true, completion: nil)
self.takePicture()
}

现在 viewController 可以毫无问题地呈现

关于ios - UIImagePickerControllerDelegate 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com