Last active
August 16, 2019 02:29
-
-
Save Slowhand0309/669b4af2277f4a9b2aabaa6be22af2ab to your computer and use it in GitHub Desktop.
[UIImagePickerController sample] #iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plist version="1.0"> | |
<dict> | |
... | |
<key>NSPhotoLibraryUsageDescription</key> | |
<string>TODO: Write the purpose of the library</string> | |
... | |
</dict> | |
</plist> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Photos | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
private func selectFromLibrary() { | |
let status = PHPhotoLibrary.authorizationStatus() | |
switch status { | |
case .authorized: | |
showImagePicker() | |
case .notDetermined: | |
PHPhotoLibrary.requestAuthorization { status in | |
if case .authorized = status { | |
self.showImagePicker() | |
} | |
} | |
case .denied, .restricted: | |
break | |
@unknown default: | |
break | |
} | |
} | |
private func showImagePicker() { | |
let imagePicker = UIImagePickerController() | |
imagePicker.delegate = self | |
imagePicker.sourceType = .photoLibrary | |
// Change navigation bar style if needed. | |
// imagePicker.navigationBar.isTranslucent = false | |
// imagePicker.navigationBar.barTintColor = UIColor.black | |
// imagePicker.navigationBar.tintColor = UIColor.white | |
self.present(imagePicker, animated: true, completion: nil) | |
} | |
} | |
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
// MARK: - ImagePickerControllerDelegate | |
func imagePickerController(_ picker: UIImagePickerController, | |
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { | |
picker.dismiss(animated: true, completion: { | |
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { | |
// Do something... | |
picker.dismiss(animated: true, completion: nil) | |
} | |
}) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
picker.dismiss(animated: true, completion: nil) | |
} | |
// MARK: - UINavigationControllerDelegate | |
func navigationController(_ navigationController: UINavigationController, | |
didShow viewController: UIViewController, animated: Bool) { | |
viewController.navigationItem.title = "TITLE" // If needed. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment