我有UIBarButton,用户可以更改图像,只需用图像选择器选择照片或图库图像。我的问题是规模图像不好。
如果我使用AspectFit,我的UIBarButton看起来像这样:如果我用…
的 这是工作代码(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; } } } }
的 输出: 强>
据我了解你的问题。我找到了解决方案。
的 试试这个 强>
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 } }
的 产量 强>
尝试使用以下代码创建自定义按钮:
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(..) 功能
resizedImage(..)