配置:
如果是相机使用,需要在info.plist文件增加使用前应用程序说明;相机使用也是如此。(第二个是CALENDARS权限,打错了;图片是Photo Library Usage Description)
从相册选择图片:
使用UIImagePickerController进行图片选择需要实现UIImagePickerControllerDelegate, UINavigationControllerDelegate
这两个代理。因为图片选择视图控制器属于系统内定义的NavigationController。
import UIKit
class ImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var buttonSelect: UIButton!
var imageView: UIImageView!
private func initView(){
buttonSelect = UIButton(type: .system)
buttonSelect.setTitle("Select", for: UIControl.State.normal)
buttonSelect.frame = CGRect(x: ((self.view.frame.width / 2) - 35), y: (self.view.frame.width - 200), width: 70, height: 30)
buttonSelect.addTarget(self, action: #selector(selectPicture), for: UIControl.Event.touchDown)
self.view.addSubview(buttonSelect)
imageView = UIImageView(frame: CGRect(x: ((