如何将自定义 UIImage 设置为标签栏项目

how to set custom UIImage to tabbar item

我想要实现的是在标签栏项目上拥有自定义配置文件 IMG

首先:我试过直接把IMG添加到tabbar

let ProfileImg: UIImageView = {
    let Img = UIImageView(image: UIImage(named: "PlaseHolder"))
        Img.layer.cornerRadius = 20
        Img.contentMode = .scaleAspectFit
        Img.clipsToBounds = true
        Img.layer.masksToBounds = true
        Img.translatesAutoresizingMaskIntoConstraints = false
    return Img
    }()

override func viewDidLoad() {
        super.viewDidLoad()

        fetchUser()

        }

       func fetchUser() {
        
        guard let uid = Auth.auth().currentUser?.uid else {return}
        let userRef = Database.database().reference(withPath: "users").child(uid)
        
        userRef.observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot.value ?? "")
            
            let value = snapshot.value as? NSDictionary

            guard let profileImageUrl = value?["profileImageUrl"] as? String else {return}
            guard let url = URL(string: profileImageUrl) else { return }

            URLSession.shared.dataTask(with: url) { (data, response, err) in
                if let err = err {
                    print("unable to get profile image", err)
                }

                guard let data = data else {return}
                let image = UIImage(data: data)
                DispatchQueue.main.async {
                    self.tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
                    self.tabBarItem.selectedImage = image?.withRenderingMode(.alwaysOriginal)
                    
                }
                
            }.resume()
            
          }) { (err) in
              print("fail to fetch user", err)
          }
        
        }

结果是:
iphone simulator image 该图像对于标签栏项目图像来说太大

但后来我发现了另一种方法,我不能死掉或得到它,它确实添加了图像,但我无法使其适合标签栏 + 圆角半径不起作用

extension UITabBarController {
    
    func addSubviewToLastTabItem(_ imageName: UIImage) {
        if let lastTabBarButton = self.tabBar.subviews.last, let tabItemImageView = lastTabBarButton.subviews.last {
            if let accountTabBarItem = self.tabBar.items?.first {
                accountTabBarItem.selectedImage = nil
                accountTabBarItem.image = nil
            }
            let imgView = UIImageView()
            imgView.frame = tabItemImageView.frame
            imgView.layer.cornerRadius = tabItemImageView.frame.height/2
            imgView.layer.masksToBounds = true
            imgView.contentMode = .scaleAspectFill
            imgView.clipsToBounds = true
            imgView.image = imageName
            self.tabBar.subviews.last?.addSubview(imgView)
        }
    }
}

这是如何称呼它

 self.tabBarController?.addSubviewToLastTabItem(image!)

结果: iphone simulator image

请提出任何想法来完成这个

使用此扩展程序调整图片大小

extension UIImage {

    func resize(targetSize: CGSize) -> UIImage {
        return UIGraphicsImageRenderer(size:targetSize).image { _ in
            self.draw(in: CGRect(origin: .zero, size: targetSize))
        }
    }
}

这里是如何在 dispatch 方法中使用它

DispatchQueue.main.async { [weak self] in
     guard let data = data else {return}
     self?.ProfileImg.image = UIImage(data: data)?.resize(targetSize: CGSize(width: 33, height: 33)).roundMyImage.withRenderingMode(.alwaysOriginal)
     self?.tabBar.items?[0].selectedImage = self?.ProfileImg.image
     self?.tabBar.items?[0].image = self?.ProfileImg.image               
      }
   }.resume()