class myViewController
{
func selectPicture()
{
self.view.endEditing(true)
let alertSheet = UIAlertController(title:nil, message: "Select your photo source", preferredStyle: .actionSheet)
alertSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
alertSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
if UIImagePickerController.isSourceTypeAvailable(.camera)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
imagePicker.navigationBar.isTranslucent = false
imagePicker.navigationBar.barTintColor = .black
imagePicker.navigationBar.tintColor = .white
imagePicker.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
self.present(imagePicker, animated: true, completion: nil)
}
}))
alertSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { action in
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) == true
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
imagePicker.navigationBar.isTranslucent = false
imagePicker.navigationBar.barTintColor = .black
imagePicker.navigationBar.tintColor = .white
imagePicker.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
self.present(imagePicker, animated: true, completion: nil)
}
}))
}
}
extension myViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
self.dismiss(animated: true) {
//****** Photo selected
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
if let imageData = UIImagePNGRepresentation(pickedImage) {
print("Total of Data: \(imageData.count)")
}
//Save photo if it was taken from camera
if picker.sourceType == .camera
{
UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)
}
}
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.