Image view Camera and Library swift 4


This how to create UIImagePicker for take foto using camera and library into imageview you can follow this step.

1. Code in your viewController

 import UIKit

class ImageProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var imageProfile: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    // Button Photo
    @IBAction func bActionUpload(_ sender: Any) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (_: UIAlertAction) in imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (_: UIAlertAction) in imagePickerController.sourceType = .photoLibrary
            self.present(imagePickerController, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    
    //did finish picked image
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
        let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageProfile.image = pickedImage
        picker.dismiss(animated: true, completion: nil)
    }
}

2. in your  Info.plist




<key>NSCameraUsageDescription</key>
<string>If Use Camera</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>If  Select From Library</string>




No comments: