首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPicker无法将选定的图像加载到UIButton

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

Stack Overflow用户
提问于 2022-09-16 08:33:27
回答 1查看 61关注 0票数 1

我以编程的方式创建了一个UIButton,它可以选择配置文件照片。所以我用PHPicker制作了图像选择器,它显示得很好。

但是在我选择图片之后,它就不会显示在按钮上了。

我想原来的照片可能是问题所在。但是新下载的照片也是一样,只是有时会显示白色背景或发送真正的错误短语。

控制台上有一些警告,比如“Xxx(文件目录)”无法打开,因为没有这样的文件。

这段PHPicker代码以前工作得很好,所以我不能假设问题出在哪里。如何选择好照片?

代码语言:javascript
复制
    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
    }()
代码语言:javascript
复制
    @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)
        
    }
代码语言:javascript
复制
// 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!!!!")
        }
    }
}

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

代码语言:javascript
复制
    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)

“”“

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-27 06:40:58

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

替换

代码语言:javascript
复制
self.plusPhotoButton.setImage(image as? UIImage, for: .normal)

使用

代码语言:javascript
复制
self.plusPhotoButton.setBackgroundImage(image as? UIImage, for: .normal)

此外,您还需要从按钮中删除图像:

代码语言:javascript
复制
self.plusPhotoButton.setImage(nil, for: .normal)

下面是您的代码,并作了一些改进:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73742072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档