在我的应用中,我有一种方法可以将UIView
转换为UIImage
。它可以在每台设备上正常工作,但iPhone 6 plus除外,它具有更高的像素密度。
我通过将灰色视图转换为selectionIndicatorImage
来为UITabBar
创建UIImage
。我知道这可能不是这种情况下的最佳实践,但无论如何我需要这种方法适用于应用程序上的其他东西。
这是我的UIImage
初始化程序:
convenience init?(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
var img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(CGImage: img.CGImage)
}
这会产生以下结果:
iPhone 6 Plus
iPhone 6
有什么想法吗?
干杯
答案 0 :(得分:0)
//寻找Line.Hope这可以帮助你
“最简单的方法是设置UIImageView的框架并将contentMode设置为其中一个” The simplest way to resize an UIImage?
答案 1 :(得分:0)
假设您正在viewDidLoad方法中计算tabBarFrame,当尚未创建标签栏时,frame属性将给出错误的结果。您需要在viewDidAppear方法中移动代码,或者如果您想更早地进行计算,您也可以在viewWillAppear中移动它,但先调用tabBar.layoutIfNeeded()