简体   繁体   English

从UIImagePickerController()选取图像后,我尝试显示UIAlertViewController(),但它没有出现并锁定屏幕

[英]After picking image from UIImagePickerController() I try to show a UIAlertViewController() but it does not appear and locks the screen

After as user picks an image from a photo album, they are prompted to enter a value into a UIAlertController. 当用户从相册中选择图像后,系统会提示他们在UIAlertController中输入值。 However, the controller doesn't appear and the screen becomes unclickable. 但是,控制器不会出现,并且屏幕变得不可点击。

It's strange as I use the exact same code for when a user takes a photo. 奇怪的是,我在用户拍摄照片时使用完全相同的代码。

The code works when the self.getVal() method is not called. 当未调用self.getVal()方法时,该代码有效。 It works as expected. 它按预期工作。 With logging, there are no errors in the code. 使用日志记录,代码中没有错误。 Basically it's like the AlertController has been created behind the current viewController view. 基本上,这就像AlertController是在当前viewController视图的后面创建的。 The screen becomes totally unclickable as it's not been dismissed. 屏幕没有被关闭,因此变得完全不可点击。 (That's my two pence) (那是我的两便士)

See code below: 参见下面的代码:

@IBAction func addBarButtonPressed(sender: UIBarButtonItem) {

    var alert = UIAlertController(title: "Camera or Existing Photo", message: "Take a photo with the camera, or use an existing photo on your device.", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: { (action: UIAlertAction!) in
        self.presentCamera()
    }))

    alert.addAction(UIAlertAction(title: "Photos", style: .Default, handler: { (action: UIAlertAction!) in
        self.presentPhotos()
    }))

    presentViewController(alert, animated: true, completion: nil)

}

func presentPhotos(){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePickerController.mediaTypes = [kUTTypeImage as String]
        imagePickerController.allowsEditing = false

        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }
}

func presentCamera() {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        imagePickerController.mediaTypes = [kUTTypeImage as String]
        imagePickerController.allowsEditing = false

        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    if (picker.sourceType == UIImagePickerControllerSourceType.Camera || picker.sourceType == UIImagePickerControllerSourceType.SavedPhotosAlbum)
    {
        let myImageName = NSUUID().UUIDString
        let imagePath = ImageHelperService.fileInDocumentsDirectory(myImageName)

        if ImageHelperService.saveImage(image, path: imagePath) {

            // TODO:
            // Error: This right here, when removed it runs and dismisses and updates fine, just the alertcontroller never appears so can never be dismissed
            self.getVal()

            while(!self.alertViewDismissed) {
                NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 0.1))

            }

            let success = DataOperationsService().SaveThingyMaBob(myImageName, value: self.alertViewValue!, bar: self.bar!)            
            if success {

                self.tableView.reloadData()
                self.doChart()

            } else {
                // TODO: Handle error
                debugPrint("Error, save failed")
            }

        } else {
            print("image save failed")
        }        
    }
}

func getVal() {
    self.alertViewDismissed = false

    let alert = UIAlertController(title: "Enter a value", message: "val", preferredStyle: .Alert)

    alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
        textField.text = ""
        textField.keyboardType = .DecimalPad
    })

    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
        print("Skip")
        self.alertViewValue = nil
        self.alertViewDismissed = true
    }))

    alert.addAction(UIAlertAction(title: "Submit", style: .Default , handler: { (action) -> Void in
        let textField = alert.textFields![0] as UITextField
        print("Text field: \(textField.text)")
        self.alertViewValue = UtilitiesService.textFieldUnwrapToDouble(textField.text!)
        self.alertViewDismissed = true

    }))

    self.presentViewController(alert, animated: true, completion: nil)
}

I resolved this by placing the call to the UIAlertController before picking the image. 我通过在选择图像之前将对UIAlertController的调用来解决此问题。

This isn't an exact fix, but more of a workaround if you can be flexible as to what you show first. 这不是一个确切的解决方法,但是如果您可以灵活选择首先显示的内容,则可以采用更多解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用UIImagePickerController选择照片后显示“预览”屏幕 - How to show the 'preview' screen after picking a photo using UIImagePickerController UIImageView不显示来自UIImagePickerController的图像 - UIImageView does not show Image from UIImagePickerController UIImagePickerController不显示编辑屏幕 - UIImagePickerController does not show edit screen 通过UIImagepickerController从Camera拾取图像并将其保存到ios中的相册后,获取图像名称 - Get image name after picking image from Camera through UIImagepickerController and saving it to photos album in ios UIImagePickerController无法在iOS 9中选择图像 - UIImagePickerController not picking image in iOS 9 UIImagePickerController不选择图像 - UIImagePickerController not picking the image 使用UIImagePickerController拾取后如何预览自动编辑的图像 - How to preview automatically edited image after picking with UIImagePickerController 拍照后在UIImagePickerController上显示预览图像 - Show preview image on UIImagePickerController after picture took imageView不显示UIImagePickerController中的图像 - imageView doesn't show image from UIImagePickerController 图像选择器/UIImagePickerController 显示空白屏幕/NavigationController swift 5 - image picker/UIImagePickerController show blank screen/NavigationController swift 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM