通过writeToFile以编程方式将视网膜和非视网膜图像添加到本地商店?

时间:2012-12-20 19:08:02

标签: objective-c ios image save nsfilemanager

您好:我的应用程序中有一个照片上传器,可让玩家上传自己的照片,以便在我的应用程序中使用。我裁剪并调整每个上传图像的大小以创建两个图像:X-by-X像素图像(用于非视网膜显示)和2X×2X像素图像(用于视网膜显示)。

然后我通过[photoDataNonRetina writeToFile:pathNonRetina atomically:YES][photoDataRetina writeToFile:pathRetina atomically:YES]将两张图片保存到本地播放器的私人文档目录,其中photoDataNonRetina和photoDataRetina是NSData对象,每张图片的文件名是photo.png和{{1}分别。

我应该如何从本地私人文件目录中检索我的图像,以便根据设备是否有视网膜显示来检索相应的图像?现在我猜想做一些类似于以下的事情:

photo@2x.png

除了看起来只是加载非视网膜图像?

3 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

也许这个?

if ([[UIScreen mainScreen].scale > 1) {
  NSString *path = [[self pathForPlayer:player] stringByAppendingPathComponent:@"photo@2x.png"];
  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];
}

答案 2 :(得分:0)

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

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