PHPickerViewController 无法将选定的图像加载到 UIButton

问题描述 投票:0回答:1

我以编程方式创建了一个 UIButton,它可以选择个人资料照片。 所以我用PHPicker做了图像选择器,它显示得很好。

但是当我选择图像后,它没有显示在按钮上。

我认为原始照片可能是问题所在。但与新下载的照片相同,只是有时会显示白色背景或发送真正的错误短语。

控制台上有一些警告,如“Xxx(文件目录)’无法打开,因为没有该文件”

这个 PHPicker 代码之前运行良好,所以我无法假设问题是什么。 如何才能选好照片?

    private let plusPhotoButton: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(UIImage(named: "plus_photo"), for: .normal)
        button.tintColor = .white
        button.addTarget(self, action: #selector(imagePickerTapped), for: .touchUpInside)
        return button
    }()
    @objc func imagePickerTapped() {
        print("imagePickerTapped")
        var configuration = PHPickerConfiguration()
        configuration.selectionLimit = 1
        
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        self.present(picker, animated: true, completion: nil)
        
    }
// MARK: - PHPicker extention
extension RegistrationController : PHPickerViewControllerDelegate {
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {

        picker.dismiss(animated: true)
        
        let itemProvider = results.first?.itemProvider
        
        if let itemProvider = itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
                DispatchQueue.main.async {
                    self.plusPhotoButton.layer.cornerRadius = self.plusPhotoButton.frame.width / 2
                    self.plusPhotoButton.layer.masksToBounds = true
                    self.plusPhotoButton.layer.borderColor = UIColor.black.cgColor
                    self.plusPhotoButton.layer.borderWidth = 2
                    self.plusPhotoButton.setImage(image as? UIImage, for: .normal)
                    
                }
            }
        } else {
            print("Image wasn't loaded!!!!")
        }
    }
}

我在下面为这个按钮添加了框架设置

    view.addSubview(plusPhotoButton)
    plusPhotoButton.centerX(inView: view)
    plusPhotoButton.setDimensions(height: 140, width: 140)
    plusPhotoButton.anchor(top: view.safeAreaLayoutGuide.topAnchor, paddingTop: 32)
    
    let stack = UIStackView(arrangedSubviews: [emailTextField, passwordTextField, fullnameTextField, UsernameTextField, loginButton])
    stack.axis = .vertical
    stack.spacing = 20
    view.addSubview(stack)
    stack.anchor(top: plusPhotoButton.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 32, paddingLeft: 32, paddingRight: 32)

'''

swift uibutton uiimage phpickerviewcontroller
1个回答
0
投票

检查代码后,您需要对代码进行以下更改:

更换

self.plusPhotoButton.setImage(image as? UIImage, for: .normal)

self.plusPhotoButton.setBackgroundImage(image as? UIImage, for: .normal)

您还需要使用以下方法从按钮中删除图像:

self.plusPhotoButton.setImage(nil, for: .normal)

这是您的代码,略有改进:

guard let img = image as? UIImage else { return }
self.profileImage = img
self.plusPhotoButton.layer.cornerRadius = self.plusPhotoButton.frame.width / 2
self.plusPhotoButton.layer.masksToBounds = true
self.plusPhotoButton.layer.borderColor = UIColor.black.cgColor
self.plusPhotoButton.layer.borderWidth = 2
self.plusPhotoButton.setBackgroundImage(img, for: .normal)
self.plusPhotoButton.setImage(nil, for: .normal)
© www.soinside.com 2019 - 2024. All rights reserved.