swift - UIImage从对象c到swift并且图像大小不好

标签 swift uiimageview uiimage resize image-resizing

我有一个 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/

相关文章:

ios - 在 View 可见之前执行Segue

android - 在 View 上显示图像

ios - 如何在 iOS 中将 UIImage 对象添加到 NSMutableArray

xcode - 在 xcode 3.2.3 中为 iPhone 本地化图像时出现问题

ios - 黑白 UIImage 中的连通区域检测(blob 检测)

ios - 防止输入文本缩放

ios - 重用自定义 Collection View 单元格保留旧的大小属性

objective-c - 沿着 UIBezierPath 将 UIImage 分成两部分

ios - 如何在 Swift 中将 NSMutableArray 作为对 NSMutableDictionary 部分的引用传递?

ios - CGContext 错误仅在模拟器中?