【问题标题】:How to display high-resolution images in UIImageView如何在 UIImageView 中显示高分辨率图像
【发布时间】:2012-07-16 17:39:42
【问题描述】:

默认情况下,UIImageView 在 UIImageView 中将他的图像显示为 1px of image = 1pt,但我想显示为 2px of image = 1pt。

使用名称“..@2x..”保存图像的版本不合适,图像未保存在文件系统中。

例如,图像尺寸为 400x100,我想将图像显示在显示器的中心,它应该是图像左侧 120 pt 和右侧 120 pt (640-400) / 2

【问题讨论】:

  • 也许你应该根据图像大小调整 UIImageView 的框架。
  • 我无法更改 UIIMageView 的框架。 UIImageView 的内容模式是 UIViewContentModeCenter。例如,图像大小为 400x100,我想将其显示在显示器的中心,图像的左右各有 120 pt。 (640-400) / 2
  • 您是否尝试在文件名末尾添加“@”符号?保存到文件系统时喜欢这个“myfile@test.meta@”吗?
  • @Eugene,图像未保存到文件系统
  • 按照建议设置框架,不要忘记相应地设置内容模式。 UIViewContentModeScaleAspectFit 可能适合您的需求。

标签: iphone objective-c ios


【解决方案1】:

这很简单,注意对于iOS 4以下的版本你没有Retina显示器,这就是为什么在图像缩放方法中我首先做这个检查:

//Retina detect
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){

然后从某处(文件、缓存等)加载image,我正在以这种方式对其进行缩放

UIImage * image2Xscaled = [UIImage alloc];
            image = [[image2Xscaled initWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp] autorelease];

方法

initWithCGImage:scale:orientation:

适用于 iOS 4.0。这就是为什么你需要第一次检查。如果不支持销售,则返回 1.0 缩放的图像。

【讨论】:

    【解决方案2】:

    你可以这样使用:

    NSData *data = //your data, however you get that...
    UIImage *image = [UIImage imageWithData:data];
    if ([UIScreen mainScreen].scale > 1.0f)
    {
        image = [UIImage imageWithCGImage:image
                                    scale:[UIScreen mainScreen].scale
                              orientation:UIImageOrientationUp]
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多