2012-12-20 46 views
0

你好:我在我的应用程序中有一个照片上传器,可以让玩家上传他们自己的照片供我的应用程序使用。我裁剪和调整每个上传的图像以创建两个图像:一个X-X像素图像(用于非视网膜显示)和一个2X像素图像(用于视网膜显示)。以编程方式将视网膜和非视网膜图像通过writeToFile添加到本地商店?

我然后通过[photoDataNonRetina writeToFile:pathNonRetina atomically:YES][photoDataRetina writeToFile:pathRetina atomically:YES]其中photoDataNonRetina和photoDataRetina是NSData的对象,并为每个图像文件名分别为photo.png[email protected]节省图像本地玩家的私人文件目录。

我应该如何随后从本地Private Documents目录中检索我的图像,以便根据设备是否具有视网膜显示来检索合适的图像?现在,我猜想做下面的内容类似:

NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo.png"]; 
return [UIImage imageWithContentsOfFile:path]; 

不同的是仅似乎加载非视网膜图像?

+1

为什么你不试试 –

+0

你的问题是什么? – 2012-12-20 19:14:24

+1

为什么要创建两种尺寸的图像?给定的设备只需要两种尺寸中的一种。只需创建当前设备所需的大小(X或2X)。 – rmaddy

回答

0

试试这个:

if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]   && 
([UIScreen mainScreen].scale == 2.0)) { 
    // Retina display 
} else { 
    // non-Retina display 
} 
+0

检查主屏幕是否响应'displayLinkWithTarget:selector:'方法的目的是什么?这与这个问题有什么关系? – rmaddy

0

也许这?

if ([[UIScreen mainScreen].scale > 1) { 
    NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"[email protected]"]; 
    UIImage * image = [UIImage imageWithContentsOfFile:path]; 
    UIImage * retinaImage = [UIImage imageWithCGImage:[image CGImage] scale:2 orientation:[image imageOrientation]]; 
    return retinaImage; 
} else { 
    NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo.png"]; 
    return [UIImage imageWithContentsOfFile:path]; 
} 
0

只需从文件名中删除扩展名即可。

NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo"]; 
return [UIImage imageWithContentsOfFile:path]; 
+0

这看起来很有前途!我必须尝试一下。我会做更新。 –

相关问题