Skip to content

Selecting an image with ImagePickerController(Swift4.2)

   

# Swift 4.2

Read the Swift3 article here.
Selecting images with UIImagePickerController

It is a sample program to get an image from the photo library of the iPhone.
In order to select images from the photo library, the project's info.plist should state that the photo library will be used.
If you don't write this, UIImagePickerController won't work.
Use the following values for the key
NSPHOtoLibraryUsageDescription. The Type is a String and the reason for use is written in the Value.
I've heard that if you don't write the reason for using it, it will be rejected at the time of examination.

alt

import UIKit
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker: UIImagePickerController!
var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerController.SourceType.photoLibrary
picker.allowsEditing = true // Whether to make it possible to edit the size etc after selecting the image
// set picker's navigationBar appearance
picker.view.backgroundColor = .white
picker.navigationBar.isTranslucent = false
picker.navigationBar.barTintColor = .blue
picker.navigationBar.tintColor = .white
picker.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
] // Title color
button = UIButton()
button.addTarget(self, action: #selector(touchUpInside(_:)), for: UIControl.Event.touchUpInside)
let size = view.frame.width * 0.8
button.setTitle("Push Me", for: UIControl.State.normal)
button.frame.size = CGSize(width: size, height: size)
button.titleLabel?.font = UIFont.systemFont(ofSize: 28)
button.center = view.center
button.backgroundColor = .orange
view.addSubview(button)
}
@objc func touchUpInside(_ sender: UIButton) {
// show picker modal
present(picker, animated: true, completion: nil)
}
// MARK: ImageVicker Delegate Methods
// called when image picked
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
button.setBackgroundImage(editedImage, for: .normal)
} else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
button.setBackgroundImage(originalImage, for: .normal)
}
dismiss(animated: true, completion: nil)
}
// called when cancel select image
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// close picker modal
dismiss(animated: true, completion: nil)
}
}

  1. Function(Swift4.2)
  2. Array(Swift4.2)
  3. Print(Swift4.2)
  4. Search the contents of UITableView with UISearchBar(Swift4)
  5. Obtaining location information while using the app
  6. The interaction of the Lifesum app was nice, so I tried to recreate it.
  7. 押すとクニュっとするボタンを実装する