ios - UIBarButtonItem 自定义 View aspect fill swift 4

标签 ios swift xcode

我正在开发一个应用程序,在主 ViewController 中,在圆形 UIBarButtonItem 中显示用户的个人资料图片,因此我使用了带有 cornerRadius 的自定义按钮并且启用了 clipsToBounds,我将 UIImage 的宽度调整为 NavigationBar 高度的 75% 以适应它,我还使用了button.imageView?.contentMode = .scaleAspectFill

当我使用方形图像(宽度 = 高度)时效果很好,但如果我使用纵向或横向图像,它看起来就像 BarButton 使用 .scaleAspectFit

我已经尝试先创建一个方形的 UIImage 裁剪原始个人资料图像,但没有任何运气。

这是我的栏按钮代码:

func setProfileButton() {
    let width = self.navigationController!.navigationBar.frame.size.height * 0.75

    if let image = ResizeImage(CFUser.current!.getProfileImage(), to: width) {
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        button.imageView?.contentMode = .scaleAspectFill
        button.addTarget(self, action: #selector(goProfile), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: width, height: width)
        button.layer.cornerRadius = button.bounds.width / 2
        button.clipsToBounds = true

        let barButton = UIBarButtonItem(customView: button)

        self.navigationItem.rightBarButtonItem = barButton
    }
}

这是 ResizeImage 代码:

func ResizeImage(_ image: UIImage, to width: CGFloat) -> UIImage? {
    let size = image.size
    let ratio = width / image.size.width
    let height = image.size.height * ratio
    let targetSize = CGSize(width: width, height: height)

    let widthRatio  = targetSize.width  / image.size.width
    let heightRatio = targetSize.height / image.size.height

    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio,height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    }

    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

这是处理方形图像的应用

Here is the app working with squared image

这是一张人像图片

And this is with a portrait image

感谢您的帮助! :)

PD:我正在使用 Xcode 10 和 Swift 4

最佳答案

由于纵向图像与方形图像不同,具有不同的尺寸,因此计算最小尺寸并相应地修改图像尺寸。

将方法 ResizeImage(_ image: UIImage, to width: CGFloat) -> UIImage? 替换为以下内容。

func ResizeImage(_ image: UIImage, to width: CGFloat) -> UIImage? {

    let ratio = width / image.size.width
    let height = image.size.height * ratio
    let dimension = min(width, height)
    let targetSize = CGSize(width: dimension, height: dimension)

    let rect = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)

    UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

方形图片:

enter image description here

人像图片:

enter image description here

关于ios - UIBarButtonItem 自定义 View aspect fill swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121857/

相关文章:

javascript - wkWebView 播放本地音频失败

iphone - 如何以编程方式闪烁屏幕?

ios - FBLikeControl 回调

android - ionic 推送通知 : Not receiving in IOS

ios - 在 UIAlertController Action 中关闭 UIViewController 会导致崩溃

swift - 如何在 Xcode 中关闭颜色文字?

ios - 如何解析具有多个值的 json?

ios - 像 Battery Doctor 这样的应用程序使用什么 API 来释放 RAM

c++ - Xcode 中基于文件类型的奇怪语法错误

ios - 无法弄清楚为什么过早释放对象