简体   繁体   English

PHPicker 无法将所选图像加载到 UIButton

[英]PHPicker can't load selected image to UIButton

I created a UIButton programmatically Which can pick profile photo.我以编程方式创建了一个 UIButton 可以选择个人资料照片。 So I made image picker with PHPicker, and it shows well.所以我用 PHPicker 制作了图像选择器,它显示得很好。

But after I selected image, it doesn't show on the button.但是在我选择图像后,它不会显示在按钮上。

I thought the original photos could be the problem.我认为原始照片可能是问题所在。 But it's the same with the newly downloaded photo except it sometimes shows white background or send real error phrase.但与新下载的照片相同,只是有时会显示白色背景或发送真正的错误短语。

There is some warning on console like "Xxx(file directory)' couldn't be opened because there is no such file"控制台上有一些警告,例如“无法打开 Xxx(文件目录),因为没有这样的文件”

This PHPicker code worked well before, so I can't assume what is the problem.这个 PHPicker 代码之前运行良好,所以我不能假设是什么问题。 How can I select photo well?我怎样才能把 select 照片拍好?

    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!!!!")
        }
    }
}

I added frame setting for this button below我在下面为这个按钮添加了框架设置

    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)

''' '''

After reviewing your code you need make below changes in your code:查看您的代码后,您需要对代码进行以下更改:

Replace代替

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

with

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

And you also need to remove image from button with:您还需要从按钮中删除图像:

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

And here is your code with minor improvement:这是您的代码,稍有改进:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM