Swift版本: 3.0
代码
首先在info.plist内添加两个参数如下,给足权限,否则无法打开图库 Key : Privacy - Media Library Usage Description
Value : YES [ It is not boolean,it is String ]
Key : Privacy - Photo Library Usage Description
Value : YES [ It is not boolean,it is String ]
在ViewController中改为如下代码 import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBOutlet weak var imagePicked: UIImageView!
@IBAction func add(_ sender: UIBarButtonItem) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker,animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
imagePicked.image = image
}else{
print("pick image wrong")
}
self.dismiss(animated: true,completion: nil)
}
}
实际效果
打开图库
显示到界面
参考: http://stackoverflow.com/questions/37925583/uiimagepickercontroller-crashes-app-swift3-xcode8
http://stackoverflow.com/questions/39009889/xcode-8-creating-an-image-format-with-an-unknown-type-is-an-error
完整demo百度云下载链接
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|