imageWithContentsOfFile vs imageNamed(imageWithContentsOfFile返回低质量图像) [英] imageWithContentsOfFile vs imageNamed (imageWithContentsOfFile return a low quality image )
问题描述
有一次,我将所有照片都放在了APP Bundle中。我使用imageNamed函数来获取图像。之后,我决定在应用启动时将一些图片复制到文档中。所以,我无法使用imageNamed函数来获取图像。我使用imageWithContentsOfFile来获取图像:
Once, I placed all my pictures in APP Bundle. I used imageNamed function to get an image. Later, I decided to copy some pictures to the Document when app start. So, I could not use imageNamed function to get an image any more. I used imageWithContentsOfFile to get an image:
NSString* documentsDirectoryPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
UIImage* result =[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", documentsDirectoryPath, fileName, extension]];
然而,imageWithContentsOfFile返回低质量图像(非常模糊)。
我的所有图像都是128 * 128。
我使用下面的代码检测图片的大小:
However, imageWithContentsOfFile return a low quality image (very fuzzy). all of my image are 128*128. I used following code the detect the size of the picture:
NSData * imgData = UIImagePNGRepresentation(image);
NSLog(@"size : %d",[imgData length]);
我发现imageNamed返回的图像大小是imageWithContentsOfFile的3倍。
我疯了......救救我!非常感谢...
I found that size of the image returned by imageNamed is 3 times than imageWithContentsOfFile. I got crazy ... Help me ! Thanks a lot ...
推荐答案
在 UIImage参考文档你可以看到imageNamed和imageWithContentsOfFile之间的一些区别。
In the UIImage reference documentation you can see a couple of differences between imageNamed and imageWithContentsOfFile.
- imageWithContentsOfFile不会缓存图像,也不会查找视网膜显示版本(@ 2x.png)。
- imageNamed而不是缓存图像并检查是否有@ 2x版本,以便在启用视网膜的设备中加载该图像。
知道了这一点,我能为您的问题考虑的最合理的解释是您正在使用视网膜设备并且具有相同图像的视网膜版本(@ 2x)。这可以解释为什么图像
Knowing this, the most logical explanation that I can think of for your problem is that you are using a retina device and have a retina version of that same image (@2x). That would explain why the image
这篇关于imageWithContentsOfFile vs imageNamed(imageWithContentsOfFile返回低质量图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!