UIImage - 从AppBundle加载的图像以较低的分辨率保存在App Directory中

时间:2011-11-07 03:48:09

标签: iphone objective-c ios uikit uiimage

所以我有一个用[UIImage imageNamed]加载的UIImage。然后我将该文件保存到我的文档目录中。我的问题是该文件最初有240分辨率。当我从Simulator的文档目录中检索文件时,它现在有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,也会发生同样的事情。

1 个答案:

答案 0 :(得分:0)

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

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

此外:

What dpi resolution is used for an iPhone App?