swift iOS - Swift - 显示图库/相机的图像选择器,感谢http://www.theappguruz.com/blog/user-interaction-camera-using

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift iOS - Swift - 显示图库/相机的图像选择器,感谢http://www.theappguruz.com/blog/user-interaction-camera-using相关的知识,希望对你有一定的参考价值。

public class WPLoginViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var imageHolderButton: UIButton!

    // MARK: - IBActions
    
    @IBAction func getImage(sender: AnyObject) {
        let alert:UIAlertController = UIAlertController(title: "Choose Image",
            message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        
        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { UIAlertAction in
            self.openCamera()
        }
        let gallaryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default) { UIAlertAction in
            self.openGallary()
        }
        
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        
        self.presentViewController(alert, animated: true, completion: nil)
    }
    
    // MARK: Image Pick Management
    
    func openCamera() {
        let picker = UIImagePickerController()
        
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.delegate = self
            presentViewController(picker, animated: true, completion: nil)
        }
        else {
            openGallary()
        }
    }
    
    func openGallery() {
        let picker = UIImagePickerController()
        picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        picker.delegate = self
        presentViewController(picker, animated: true, completion: nil)
    }
}

extension WPLoginViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    public func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        picker.dismissViewControllerAnimated(true, completion: nil)
        imageHolderButton.setImage(image, forState: .Normal)
    }
    
}

以上是关于swift iOS - Swift - 显示图库/相机的图像选择器,感谢http://www.theappguruz.com/blog/user-interaction-camera-using的主要内容,如果未能解决你的问题,请参考以下文章

ios8 swift开发:显示变量的类名称

Swift - GCM:不显示 iOS 远程推送通知

iOS - UIVIewController 中的 UITableView 不显示所有行 [Swift]

iOS - 如何在 Swift 中显示“AirPlay”弹出菜单?

仅在边界外显示的图像 - iOS/Swift

变量显示为 nil - swift 4 IOS