从库中更改UIBarButton图像

问题描述 投票:-1回答:3

我有UIBarButton,用户可以通过图像选择器选择照片或图库图像来更改图像。我的问题是规模图像不好。

如果我使用AspectFit,我的UIBarButton看起来像这样:

enter image description here

如果我使用AspectFill,我的UIBarButton看起来像这样:

enter image description here

如果我先尝试更改尺寸图像,并在设置后,图像始终划伤:

enter image description here

这是我的代码:

    func createPhotoBarButton(image: Data) {
        let barbtn = UIBarButtonItem()

        var image = UIImage(data:image)
        if image == nil {
            image = UIImage(named: "photoIcon")
        }

        let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))

        imageView.image = image?.resizedImage(newSize: CGSize(width: 35, height: 35))?.withRenderingMode(.alwaysOriginal)
//                imageView.image = cropToBounds(image: image!, width: 35, height: 35)

//        imageView.image = image

        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = imageView.frame.size.height / 2
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true



        self.navigationItem.rightBarButtonItem = barbtn

        barbtn.customView = imageView
        barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
    }

这里func for resize image:

func resizedImage(newSize: CGSize) -> UIImage? {
    guard size != newSize else { return self }

    let hasAlpha = false
    let scale: CGFloat = 0.0
    UIGraphicsBeginImageContextWithOptions(newSize, !hasAlpha, scale)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

    draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
    let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()


    UIGraphicsEndImageContext()
    return newImage
}

帮我找到解决问题的正确方法。

enter image description here

swift uiimageview uibarbuttonitem
3个回答
2
投票

据我了解你的问题。我找到了解决方案。

试试这个

func createPhotoBarButton(image: Data) {
        let barbtn = UIBarButtonItem()

        let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))

        var image = UIImage(data:image)
        if image == nil {
            image = UIImage(named: "photoIcon")?.resize(maxWidthHeight: Double(imageView.frame.size.width))
        }

        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = imageView.frame.size.height / 2
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true

        self.navigationItem.rightBarButtonItem = barbtn

        barbtn.customView = imageView
        barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
}

调整大小的方法

extension UIImage {

    func resize(maxWidthHeight : Double)-> UIImage? {

        let actualHeight = Double(size.height)
        let actualWidth = Double(size.width)
        var maxWidth = 0.0
        var maxHeight = 0.0

        if actualWidth > actualHeight {
            maxWidth = maxWidthHeight
            let per = (100.0 * maxWidthHeight / actualWidth)
            maxHeight = (actualHeight * per) / 100.0
        }else{
            maxHeight = maxWidthHeight
            let per = (100.0 * maxWidthHeight / actualHeight)
            maxWidth = (actualWidth * per) / 100.0
        }

        let hasAlpha = true
        let scale: CGFloat = 0.0

        UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: maxHeight), !hasAlpha, scale)
        self.draw(in: CGRect(origin: .zero, size: CGSize(width: maxWidth, height: maxHeight)))

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        return scaledImage
    }

}

产量

enter image description here


0
投票

尝试使用以下代码创建自定义按钮:

func createPhotoBarButton(image: Data) {
    let imageBtnContainer = UIButton()
    let imageBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    var image = UIImage(data:image)
    if image == nil {
        image = UIImage(named: "photoIcon")
    }
    imageBtn.setImage(image, forState: UIControlState.Normal)
    imageBtn.frame = CGRectMake(0, 0, 53, 31)
    imageBtn.imageView?.contentMode = .scaleAspectFit

    imageBtnContainer.addSubview(imageBtn)

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageBtnContainer)
    ...
}

你不再需要resizedImage(..)功能了


0
投票

这是工作代码(swift 4)

override func viewDidLoad() {

    let containView = UIView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))

    let Button : UIButton = UIButton.init(type: .custom)
    Button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
    Button.Round = true
    Button.setImage(UIImage(named: "photoIcon"), for: .normal)
    Button.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)   
    containView.addSubview(Button)
    let rightBarButton = UIBarButtonItem(customView: containView)
    self.navigationItem.rightBarButtonItem = rightBarButton
}

按钮动作在这里

@objc func btnTapped(_ sender: Any) {
    print("Click Event")
}

圆形扩展

extension UIView{

    @IBInspectable var Round:Bool{
        get{
            return false
        }
        set{
            if newValue == true {
                self.layer.cornerRadius = self.frame.size.height/2;
                self.layer.masksToBounds = true;
            }

        }
    }
}

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.