选择图片时 UIImagePickerController 冻结
UIImagePickerController freezes when selecting a picture
我正在尝试允许用户修改他们的个人资料图片。我已经实现了正确的代码并在我的 plist 中输入了正确的调用。当我选择图片时,我的应用程序崩溃了 select "choose" 我将把我的代码放在下面。
import UIKit
import Firebase
class EditProfileVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(profileImageView)
setupProfileImageView()
self.view.backgroundColor = UIColor.white
}
func setupProfileImageView() {
profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive=true
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true
}
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "users")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
imageView.isUserInteractionEnabled = true
return imageView
}()
}
我还创建了一个扩展文件
import UIKit
extension EditProfileVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@objc func handleSelectProfileImageView() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"]
as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"]
as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
profileImageView.image = selectedImage
}
print(info)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("Canceled Picker")
dismiss(animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
profileImageView.image = img
}
}
}
在我的Plist中,我在使用说明中加入了privacy-photo library。
当我 select 我的照片并按选择更新用户个人资料图片时,它崩溃并发送一条错误消息,
["UIImagePickerControllerEditedImage": size
{1122, 1125} orientation 0 scale 1.000000,
"UIImagePickerControllerImageURL":
file:///Users/zachwilcox/Library/Developer/CoreSimulator/Devices/C05956CC-616F-42FB-B067-110AF2BD7713/data/Containers/Data/Application/B386A454-C96D-447F-8FB2-5FFCCF1D1780/tmp/63AD5A2C-4214-4E45-9961-F800699B14E0.jpeg, "UIImagePickerControllerMediaType": public.image,
"UIImagePickerControllerCropRect": NSRect: {{0, 78},
{2456.9999065657889, 2458}}, "UIImagePickerControllerReferenceURL":
assets-library://asset/asset.JPG?id=0F3CDF5B-A958-45D2-822E-571323272379&ext=JPG,
"UIImagePickerControllerOriginalImage": size
{3024, 3780} orientation 0 scale 1.000000]
我不知道这意味着什么,为什么叫它或如何修复它。如果有人可以帮助我理解为什么会发生这种情况,我将不胜感激!
你有一个重复的方法。尝试从您的代码中删除以下内容。
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
profileImageView.image = img
}
}
编辑:
选择图像后关闭 imagepickercontroller。
包含在 didFinishPickingMediaWithInfo 方法的底部。
dismiss(animated: true, completion: nil)
我正在尝试允许用户修改他们的个人资料图片。我已经实现了正确的代码并在我的 plist 中输入了正确的调用。当我选择图片时,我的应用程序崩溃了 select "choose" 我将把我的代码放在下面。
import UIKit
import Firebase
class EditProfileVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(profileImageView)
setupProfileImageView()
self.view.backgroundColor = UIColor.white
}
func setupProfileImageView() {
profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profileImageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive=true
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true
}
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "users")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
imageView.isUserInteractionEnabled = true
return imageView
}()
} 我还创建了一个扩展文件
import UIKit
extension EditProfileVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@objc func handleSelectProfileImageView() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"]
as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"]
as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
profileImageView.image = selectedImage
}
print(info)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("Canceled Picker")
dismiss(animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
profileImageView.image = img
}
}
}
在我的Plist中,我在使用说明中加入了privacy-photo library。
当我 select 我的照片并按选择更新用户个人资料图片时,它崩溃并发送一条错误消息,
["UIImagePickerControllerEditedImage": size {1122, 1125} orientation 0 scale 1.000000, "UIImagePickerControllerImageURL": file:///Users/zachwilcox/Library/Developer/CoreSimulator/Devices/C05956CC-616F-42FB-B067-110AF2BD7713/data/Containers/Data/Application/B386A454-C96D-447F-8FB2-5FFCCF1D1780/tmp/63AD5A2C-4214-4E45-9961-F800699B14E0.jpeg, "UIImagePickerControllerMediaType": public.image, "UIImagePickerControllerCropRect": NSRect: {{0, 78}, {2456.9999065657889, 2458}}, "UIImagePickerControllerReferenceURL": assets-library://asset/asset.JPG?id=0F3CDF5B-A958-45D2-822E-571323272379&ext=JPG, "UIImagePickerControllerOriginalImage": size {3024, 3780} orientation 0 scale 1.000000]
我不知道这意味着什么,为什么叫它或如何修复它。如果有人可以帮助我理解为什么会发生这种情况,我将不胜感激!
你有一个重复的方法。尝试从您的代码中删除以下内容。
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
profileImageView.image = img
}
}
编辑:
选择图像后关闭 imagepickercontroller。 包含在 didFinishPickingMediaWithInfo 方法的底部。
dismiss(animated: true, completion: nil)