IOS selects the image UIImagePickerController from the user’s album
[
GitHub] iOS Demo:
https://github.com/KyleBing/iosDemo
The examples in this article can be found in the GitHub project above, and contain many other common uses
UI
Use of components
Selecting an image from the user’s album, or taking a photo, is done using UIImagePickerController
content
Select an image from the album and display it proportionally in the middle of the current ViewController
steps
VC
=
ViewController
Convenient explanation, so abbreviation
- In the current
VC
In the implementationUIImagePickerControllerDelegate
,UINavigationController
These two protocols - Create a new
picker: UIImagePickerController
和imageView: UIImageView
- Set the picker’s
delegate
Is the currentVC
- Set up the
imageView
The content display property of: Scale - Sets the trigger method for selecting a photo, such as in
NavigationBar
Add a button to -
Two methods to realize UIImagePickerControllerDelegate
- When the user selects a picture: get the information in INFO and perform related operations, and finally make the selection window disappear
- When the user deselects, the selection window disappears
The effect
Code and Comments
// // imagePickerVc. swift // iosDemo Created by Kyle on 2020/3/16. // Copyright © 2020 kylebin. All rights reserved. // import UIKit /// 0. The ViewController need to implement UIImagePickerControllerDelegate and UINavigationControllerDelegate two protocols class ImagePickerVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { /// 1. Create a new UIImagePickerController object var ImagePicker: UIImagePickerController = UIImagePickerController() lazy var imageView = UIImageView(frame: view.frame) override func viewDidLoad() { super.viewDidLoad() navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Pick", style: .plain, target: self, action: #selector(pickAnImage)) /// 2. Set picker's delegate to self imagePicker. Delegate = self imagePicker. SourceType =. PhotoLibrary // 3. ImageView.contentMode =.ScaleSpectFit View.addSubView (ImageView)} @objc Func PickAnimage (){/// 4. Display the PickerController present(ImagePicker, Animated: true) {print("UIImagePickerController: presented") } } // MARK: - Image picker delegate func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { /// 5. This method fires when the user selects an image and returns info about the selected image. Get the originImage attribute in this picture, is the picture itself guard let selectedImage = info [UIImagePickerController. InfoKey. OriginalImage] as? UIImage else { fatalError("error: did not picked a photo") } /// 7. If you want to do anything else, you can select the image here and close the Picker Controller. Picker (Animated: true) { [unowned self] in // add a image view on self.view self.imageView.image = selectedImage } } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { /// 8. Picker. dismiss(animated: true) {print("UIImagePickerController: electioneController ")}}
The info content
__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.JPG? id=557741A9-A05A-468E-AFAB-9B759CB66660&ext=JPG __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerOriginalImage): <UIImage:0x283e360a0 anonymous {2048, 1536}> __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerMediaType): public.image __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerImageURL): file:///private/var/mobile/Containers/Data/Application/A5875FF6-3D3C-4147-BF5B-AD850397694D/tmp/CC744A41-B42F-490E-9B5F- 5AF06CDD24C4.jpeg]