我有一个 PNG 图像,我会将其插入到我的导航标题中。
在对象 C 中我已经
UIImage *image = [UIImage imageNamed:@"logo_up.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
效果很好,我的图像质量很高
然后我在 swift 中创建一个类似的应用程序,使用相同的图像,并编写此代码
self.navigationItem.titleView = UIImageView(image: UIImage(named: "icons/logo_up.png"))
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.frame.size.width = 320
self.navigationController?.navigationBar.frame.size.height = 44
它打印了我的图像,但质量很差。 我创建了不同尺寸的不同图像,但没有任何变化。这个应用程序中的每张图像质量都很差。 我使用 PNG 和 JPG 扩展名。
最佳答案
尝试使用 UIImageView
的内容模式。您正在更改 Swift 示例中 UINavigationBar
的框架,这样做可能会降低图像质量。
let imageView = UIImageView(image: UIImage(named: "icons/logo_up.png"))
imageView.contentMode = .ScaleAspectFit //change contentMode
self.navigationItem.titleView = imageView
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() self.navigationController?.navigationBar.frame.size.width = 320
self.navigationController?.navigationBar.frame.size.height = 44
关于swift - UIImage从对象c到swift并且图像大小不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528783/