首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIBarButtonItem -背景图像,drawInRect和setBackgroundImage - Swift

UIBarButtonItem -背景图像,drawInRect和setBackgroundImage - Swift
EN

Stack Overflow用户
提问于 2014-08-22 01:35:05
回答 1查看 2.9K关注 0票数 0

我试图将UIBarButtonItem的背景图像设置为我已经在应用程序中的其他地方成功显示的图像,但我得到了一个奇怪的伸展,我似乎无法解决。同时,我正试着把照片裁剪成一个圆圈。

下面是我的代码:

代码语言:javascript
复制
        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()

EN

回答 1

Stack Overflow用户

发布于 2014-11-30 19:02:53

尝尝这个

代码语言:javascript
复制
   self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIGraphicsGetImageFromCurrentImageContext(), style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25432497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档