从图像选择器中选择的图像未显示

时间:2017-07-31 13:43:30

标签: ios swift

我有点迷路了。我的图像选择器正在工作,但图像未显示在我的图像视图中。我查看了我的代码和各种解决方案,但它仍然无法正常工作。我已将委托设置为self并双重检查我的方法,但它仍未显示图像所在的位置。

import UIKit

class AlterProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    view?.backgroundColor = UIColor.white
    navigationItem.title = "Profile Settings"
    view.addSubview(selectProfileImage)


    ///Constraints for all views will go here

    _ = selectProfileImage.anchor(view.centerYAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -275, leftConstant: 135, bottomConstant: 0, rightConstant: 0, widthConstant: 100, heightConstant: 100)

  //  selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width/2

    ///////////////////////////////////////////////


    // Do any additional setup after loading the view.
}



override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width / 2;
    selectProfileImage.layer.masksToBounds = true

}

//Where all buttons and labels will be added

//will just be a nice looking image view to be next to the profile settings button
lazy var selectProfileImage: UIImageView = {
    let selectPicture = UIImageView()
   // self.selectProfileImage.layer.cornerRadius = self.selectProfileImage.frame.size.width / 2;
    selectPicture.image = UIImage(named: "Paris")

    // selectPicture.layer.cornerRadius = selectPicture.frame.size.width / 2;
    selectPicture.clipsToBounds = true
    selectPicture.translatesAutoresizingMaskIntoConstraints = false
    selectPicture.layer.cornerRadius = selectPicture.frame.size.width/2
    selectPicture.contentMode = .scaleAspectFill
    selectPicture.isUserInteractionEnabled = true
    selectPicture.layer.shouldRasterize = true
    // will allow you to add a target to an image click
    selectPicture.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
    selectPicture.layer.masksToBounds = true
    return selectPicture
}()


func handleSelectProfileImageView()  {
    print("123")
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    present(picker, animated: true, completion: nil)

}

// will dispaly info of image selected
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("info")
    var selectedImageFromPicker: UIImage?
    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage{
        print((editedImage as AnyObject).size)
        selectedImageFromPicker = editedImage

    }else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
        print((originalImage as AnyObject).size)
        selectedImageFromPicker = originalImage
    }

    if let selectedImage = selectedImageFromPicker {
        selectProfileImage.image = selectedImage
    }


    dismiss(animated: true, completion: nil)

}
// will handle the picker being closed/canceled
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    print("picker canceled")
    dismiss(animated: true, completion: nil)
    }
///////////////////////////////////////////////////////////////////////////////////



   }

0 个答案:

没有答案