【问题标题】:Storing image in plist将图像存储在 plist 中
【发布时间】:2010-03-21 10:29:07
【问题描述】:

我们如何将图像存储在 plist 文件中。这个 plist 文件存储在哪里?谁能给我一个例子?答案将不胜感激!

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    UIImage 没有直接实现存储在 plist 中所需的 NSCoder 协议。

    但是,像下面这样添加是相当容易的。

    UIImage+NSCoder.h

    #import <Foundation/Foundation.h>
    
    @interface UIImage (MyExtensions)
    - (void)encodeWithCoder:(NSCoder *)encoder;
    - (id)initWithCoder:(NSCoder *)decoder;
    @end
    

    UIImage+NSCoder.m

    #import "UIImage+NSCoder.h"
    
    @implementation UIImage (MyExtensions)
    
    - (void)encodeWithCoder:(NSCoder *)encoder
    {
      [encoder encodeDataObject:UIImagePNGRepresentation(self)];
    }
    
    - (id)initWithCoder:(NSCoder *)decoder
    {
      return [self initWithData:[decoder decodeDataObject]];
    }
    
    @end
    

    添加后,您将能够使用 ie 将 UIImages 存储在 plist 中

    // Get a full path to a plist within the Documents folder for the app
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *path = [NSString stringWithFormat:@"%@/my.plist",
                                                  [paths objectAtIndex:0]];
    
    // Place an image in a dictionary that will be stored as a plist
    [dictionary setObject:image forKey:@"image"];
    
    // Write the dictionary to the filesystem as a plist
    [NSKeyedArchiver archiveRootObject:dictionary toFile:path];
    

    注意:我不建议这样做,除非你真的想这样做,即对于非常小的图像。

    【讨论】:

      【解决方案2】:

      始终将图像路径存储在 .plist 中,而不是实际图像。如果你这样做,你会受到性能打击。您希望在需要时加载图像,而不是一次全部加载。

      【讨论】:

        【解决方案3】:

        取决于应用程序知道这些图像的位置和时间。

        对于作为应用程序捆绑程序一部分的图像,here 可能会提供一些见解。如果您想在应用程序运行期间存储图像,您可以使用相同的概念(具有图像相对路径的用户 plist)。

        -- 弗兰克

        【讨论】:

          【解决方案4】:

          plist 文件旨在存储键值对,而不是应用程序的资源。图标、图像和表单通常存储在 .xib 文件中(参见 Interface Builder)

          【讨论】:

          • 非常感谢您的建议!!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-24
          • 2012-01-16
          • 2020-04-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多