在plist中存储图像

时间:2010-03-21 10:29:08

标签: iphone objective-c

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

4 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:4)

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

答案 2 :(得分:1)

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

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

- 弗兰克

答案 3 :(得分:0)

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