Using the camera
Go to info.plist and add the following keys:
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo library use
2. At class:
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate
3. Add to button click function to get camera:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
4. Add the following function to post the image to the image view:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Now image picking")
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
myImageView.image = pickedImage
dismiss(animated: true, completion: nil)
}
}
5. Save the image to the object library
let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.6)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo library use
2. At class:
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate
3. Add to button click function to get camera:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
4. Add the following function to post the image to the image view:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Now image picking")
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
myImageView.image = pickedImage
dismiss(animated: true, completion: nil)
}
}
5. Save the image to the object library
let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.6)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
Comments
Post a Comment