从库中更改 UIBarButton 图像 [英] Change UIBarButton image from gallery

查看:15
本文介绍了从库中更改 UIBarButton 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UIBarButton,用户可以更改图像,只需使用图像选择器选择照片或图库图像即可.我的问题是糟糕的比例图像.

I have UIBarButton and user can change image just chose photo or gallery image with image picker. My problem is bad scale image.

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

If I use AspectFit my UIBarButton look like this:

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

If I use AspectFill my UIBarButton look like this:

如果我先尝试更改大小图像,然后设置它,图像总是被划伤:

and if I try first change size image and after set it, image all time scratched:

这是我的代码:

    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(_:))))
    }

这里是调整图像大小的函数:

And here 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
}

请帮助我找到解决问题的正确方法.

Help me plz find the right way to resolving my problem.

推荐答案

据我了解您的问题.我找到了解决方案.

As I understand your question. I found a solution.

试试这个

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
    }

}

输出

这篇关于从库中更改 UIBarButton 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆