【问题标题】:UIImage - Image loaded from AppBundle is saved at lower resolution in App DirectoryUIImage - 从 AppBundle 加载的图像以较低的分辨率保存在 App Directory 中
【发布时间】:2011-12-23 08:51:41
【问题描述】:

所以我有一个使用[UIImage imageNamed] 加载的 UIImage。然后我将该文件保存到我的文档目录中。我的问题是该文件最初具有 240 分辨率。当我从模拟器的文档目录中检索文件时,它现在具有 72 分辨率。如何以与加载时相同的分辨率保存它?

这是我的加载方式:

UIImage * image = [UIImage imageNamed:@"halloween_big.jpg"]
CGContextRef context = CGBitmapContextCreate(
                                                     (void*) pixels,
                                                     image.size.width,
                                                     image.size.height,
                                                     8,
                                                     image.size.width * 4,
                                                     CGImageGetColorSpace(image.CGImage),
                                                     kCGImageAlphaPremultipliedLast
                                                     );

//  get the image from the current context with our new pixels
CGImageRef imageRef = CGBitmapContextCreateImage( context );

image = [UIImage imageWithCGImage:imageRef];

编辑 - 我也尝试过仅使用此加载图像并且不对图像进行任何更改:

 UIImage *loaded = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"halloween_big" ofType:@"jpg"]];  

这是我保存它的方法:

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

编辑 - 如果我将文件另存为 PNG,我会遇到同样的分辨率损失。

可以肯定的是,如果我将质量参数更改为 UIImageJPEGRepresentation,也会发生同样的事情。

【问题讨论】:

  • 如果我在加载图像后立即保存图像而不处理图形上下文,我会得到相同的结果。
  • 这可能是解决方案。看看这里:stackoverflow.com/questions/990550/…
  • 您能否详细说明一下 - 我不是要更改图像的大小,我只是希望保存的图像与我最初打开的图像具有相同的分辨率。

标签: iphone objective-c ios uikit uiimage


【解决方案1】:

iPhone 很可能会为您的模拟器加载具有优化分辨率的图像。

您是否在模拟器的非视网膜显示版本上进行测试?

还有:

What dpi resolution is used for an iPhone App?

【讨论】:

  • 我在 iPhone 模拟器中尝试了使用和不使用 Retina。在对图像进行任何操作之前,我也将其追溯到。一旦它被加载为 UIImage - 我将它写入磁盘并且其分辨率为 72。我尝试使用 imageNamed 和 initWithContentsOfFile 加载 UIImage。
  • 您在实际设备上看到不同的结果吗?
  • 我还没有在设备上尝试过。我想这是一种可能。我仍处于早期开发阶段,正在尝试制作原型。
猜你喜欢
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2018-05-02
  • 2017-08-23
  • 1970-01-01
  • 2015-03-12
  • 2021-08-26
  • 1970-01-01
相关资源
最近更新 更多