gpt4 book ai didi

ios - UIBarButtonItem - 背景图像、drawInRect 和 setBackgroundImage - Swift

转载 作者:行者123 更新时间:2023-11-28 10:26:03 25 4
gpt4 key购买 nike

我正在尝试将 UIBarButtonItem 的背景图像设置为我已在应用程序的其他位置成功显示的图像,但我遇到了一个我似乎无法解决的奇怪问题。同时,我尝试将照片裁剪成圆形。

这是我的代码:

        var profileView : UIView = self.profilePhoto.valueForKey("view") as UIView
var profileImage = (user.valueForKey("profile_photo") as UIImage)
var width = profileView.bounds.size.width
var size = CGSize(width: width, height: width)

UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
UIBezierPath(roundedRect: profileView.bounds, cornerRadius: width / 2).addClip()
profileImage.drawInRect(profileView.bounds)

println("width: \(profileImage.size.width), height: \(profileImage.size.height)")
// Show image without any cropping
//self.profilePhoto.setBackgroundImage(profileImage, forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
self.profilePhoto.setBackgroundImage(UIGraphicsGetImageFromCurrentImageContext(), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
UIGraphicsEndImageContext()

Image with cropping

Stretched Image no cropping

最佳答案

试试这个

   self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIGraphicsGetImageFromCurrentImageContext(), style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

关于ios - UIBarButtonItem - 背景图像、drawInRect 和 setBackgroundImage - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com