【问题标题】:How to get image without resizing?如何在不调整大小的情况下获取图像?
【发布时间】:2016-02-24 12:45:58
【问题描述】:

首先我需要描述我要做什么:

  1. 我需要为所有设备显示一个全屏宽度的标题图像。
  2. 我应该提供多张不同尺寸的图片,而不仅仅是 2x 和 3x。

所以我会有这些图片:

  1. header_width_1242.png for iphone 6 plus.

  2. header_width_1125.png for iphone 6 plus display zoom

  3. header_width_640.png for iphone 5,6

    ...

所以我不应该根据比例选择图像,而是应该根据宽度选择图像:

let image_name = "header_width_" + String(UIScreen.mainScreen().scale * UIScreen.mainScreen().bounds.width)

let image = UIImage(named:image_name)

问题,ios 再次自动缩放图像。因此,如果设备具有 2 倍的比例。然后它返回图像 * 2 大小。

例如:对于宽度为 320 和比例为 2 的 iphone 5,我需要 header_width_640.png,但系统似乎将图像缩放到 1280 (640 * 2)。

我如何告诉系统返回图像UIImage(named:image_name) 而不缩放?谢谢

【问题讨论】:

  • 这是怎么回事?

标签: ios ios8 uiimage ios9


【解决方案1】:

您可以在 viewController 上声明 screenSize 属性。

// Determine screen size
let screenSize: CGRect = UIScreen.mainScreen().bounds

然后,当您需要设置图像时,您可以执行以下操作:

if screenSize.width < 641 {
    // set your imageView to the image for 640 screen width 
} else if screenSize.width < 1126 {
    // set your imageView to the image for 1125 screen width 
} else if screenSize.width < 1243 {
    // set your imageView to the image for 1242 screen width
}

【讨论】:

    【解决方案2】:

    您不应该通过屏幕尺寸来限制资产,如果可以的话,最好使用尺寸等级。图像资产目录通过为每个图像选择大小类别,在检查器面板中实现了这一点。
    我确实理解有时是需要的,但请尝试从相对角度思考。
    如果图像是左右对齐的徽标,您可以使用切片在图像上创建可拉伸的结束/开始。
    如果图像以纯色或可通过代码绘制的东西居中,您可以在运行时绘制它。
    这是 ObjC 中的一个 sn-p,在我的应用程序中使用,您可以在 SWIFT 中轻松转换:

    -(UIImage*) createNavBarBackgroundWithImage:(UIImage*) image {
        CGSize  screenSize = ((CGRect)[[UIScreen mainScreen] bounds]).size;
        CGFloat width = screenSize.width;
        CGFloat height = 64.0;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
        //Draw background
        UIColor * backGroundColor = [UIColor colorWithRed:27.f/255.f green:142.f/255.f blue:138.f/255.f alpha:1.0];
        [backGroundColor setFill];
        UIRectFill(CGRectMake(0, 0, width, height));
        //Draw the image at the center
        CGPoint point = CGPointMake(width/2 - image.size.width/2, 0);
        [image drawAtPoint:point];;
    
        UIImage *newBGImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newBGImage;
    } 
    

    【讨论】:

      【解决方案3】:

      使用图片资源

      https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/chapters/AddingImageSets.html

      1. header_width_1242.png 适用于 iphone 6 plus。将其命名为 header_width_414.png 拖动到 3x
      2. header_width_1125.png 将其命名为 header_width_375.png 拖动到 3x
      3. header_width_640.png 将其命名为 header_width_320.png 拖动到 2x

        让 image_name = "header_width_" + UIScreen.mainScreen().bounds.width)

        让 image = UIImage(named:image_name)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多