【问题标题】:Programmatically add retina and non-retina images to local store via writeToFile?通过 writeToFile 以编程方式将视网膜和非视网膜图像添加到本地存储?
【发布时间】:2012-12-20 19:08:01
【问题描述】:

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

然后我通过[photoDataNonRetina writeToFile:pathNonRetina atomically:YES][photoDataRetina writeToFile:pathRetina atomically:YES] 将这两个图像保存到本地播放器的Private Documents 目录,其中photoDataNonRetina 和photoDataRetina 是NSData 对象,每个图像的文件名分别是photo.pngphoto@2x.png

我应该如何随后从本地 Private Documents 目录中检索我的图像,以便根据设备是否具有 Retina 显示器检索适当的图像?现在我猜想做一些类似于以下的事情:

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

除了这似乎只加载非视网膜图像?

【问题讨论】:

  • 为什么要创建两种尺寸的图像?给定的设备只需要两种尺寸中的一种。只需创建当前设备所需的大小(X 或 2X)即可。
  • 除非您对图像使用 iCloud 备份并在具有多个缩放图像(视网膜、iPad 等)的多台设备上使用,否则我会支持仅为您当前设备创建缩放图像并保存的动作多张图片所需的存储空间。
  • 最终是的,我希望能够跨设备共享照片。

标签: objective-c ios image save nsfilemanager


【解决方案1】:

试试这个:

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

【讨论】:

  • 检查主屏幕是否响应displayLinkWithTarget:selector:方法的目的是什么?这与这个问题有什么关系?
【解决方案2】:

也许是这个?

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];
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 这看起来很有希望!我得试试看。我会更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多