2017-06-01 49 views
0

我具有被編程的容器視圖中創建這樣的圖像視圖:從imagePickerController選擇的圖像的UI圖像視圖內佔據整個屏幕,而不是嵌合[夫特]

Screen shot with circular profile image view inside container view

我有一個按鈕覆蓋在配置文件圖像視圖頂部以調用圖像選擇器控制器。當從照片庫中選擇一個圖像時,圖像佔用如下整個屏幕:

Screenshot after image is selected

下面是該視圖控制器的viewDidLoad()和imagePickerController()的代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.profileBackgroundView.backgroundColor = UIColor.init(red: CGFloat(69.0/255.0), green: CGFloat(171.0/255.0), blue: CGFloat(222.0/255.0), alpha: CGFloat(1)) 

    //initializePrimaryUserData() 

    fullNameTextField.text = self.primaryUser.firstName + self.primaryUser.lastName 
    nickNameTextField.text = self.primaryUser.userName 

    //create profile image view 

    if hasProfilePic { 
     profileImage.image = self.primaryUser.image 
    } else { 
     image = UIImage(named: imageName)! 
     profileImage.image = image 
    } 

    profileImage.frame = CGRect(x: profileBackgroundView.bounds.midX, y: profileBackgroundView.bounds.midY, width: 100, height: 100) 

    profileImage.translatesAutoresizingMaskIntoConstraints = false 
    profileBackgroundView.addSubview(profileImage) 

    profileImage.centerXAnchor.constraint(equalTo: profileBackgroundView.centerXAnchor).isActive = true 
    profileImage.centerYAnchor.constraint(equalTo: profileBackgroundView.centerYAnchor).isActive = true 

    profileImage.layer.cornerRadius = profileImage.frame.size.width/2 
    profileImage.layer.masksToBounds = false 
    profileImage.clipsToBounds = true 
    profileImage.contentMode = .scaleAspectFit 

    //create profileImageButton 
    let profileImageButton = UIButton(frame: CGRect(x: profileBackgroundView.bounds.midX, y: profileBackgroundView.bounds.midY, width: 100, height: 100)) 
    profileImageButton.setTitle("Photo", for: .normal) 
    profileImageButton.addTarget(self, action: #selector(profileImageButtonClicked), for: .touchUpInside) 

    profileImageButton.translatesAutoresizingMaskIntoConstraints = false 

    profileBackgroundView.addSubview(profileImageButton) 

    profileImageButton.centerXAnchor.constraint(equalTo: profileBackgroundView.centerXAnchor).isActive = true 
    profileImageButton.centerYAnchor.constraint(equalTo: profileBackgroundView.centerYAnchor).isActive = true 

    // Do any additional setup after loading the view. 

} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 

     self.primaryUser.image = image 
     self.hasProfilePic = true 
     self.profileImage.image = image 
     self.profileImage.contentMode = .scaleAspectFit 
     self.profileImage.clipsToBounds = true 
    } 

    self.dismiss(animated: true, completion: nil) 
} 

回答

0

你可以試試這個

self.profileImage.contentMode = .scaleToFill 
+0

我試過了,但也沒有工作。 – Abe