选择图片时UIImagePickerController冻结

时间:2018-07-02 00:23:06

标签: ios swift xcode uiimagepickercontroller

我正试图允许用户修改其个人资料图片。我已经实现了正确的代码,并在我的plist中输入了正确的调用。选择图片并选择“选择”时,我的应用程序崩溃,我将在下面放置代码。

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
    }
}
}

在我的列表中,我在用法说明中放入了隐私照片库。

当我选择我的图片并按选择以更新用户个人资料图像时,它崩溃并发送错误消息,

  

[“ UIImagePickerControllerEditedImage”:大小   {1122,1125}方向0比例1.000000,   “ UIImagePickerControllerImageURL”:   文件:/// 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”:   asset-library://asset/asset.JPG?id = 0F3CDF5B-A958-45D2-822E-571323272379&ext = JPG,   “ UIImagePickerControllerOriginalImage”:大小   {3024,3780}方向0比例1.000000]

我不知道这意味着什么,为什么要调用它或如何解决它。如果有人可以帮助我理解为什么会这样,我将不胜感激!

1 个答案:

答案 0 :(得分:1)

您有一个重复的方法。尝试从代码中删除以下内容。

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)