将图像存储在plist中

我们如何将图像存储在plist文件中。 这个plist文件在哪里存储? 任何人都可以给我一个例子吗? 答案将不胜感激!

UIImage不直接实现在plists中存储所需的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 

当这已经被添加,你将能够存储UIImage与plist与ie

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

注:我不build议这样做,除非你真的想要,即真正的小图像。

总是将图像path存储在.plist中,而不是实际的图像。 如果你这样做,你会赢得一场表演。 您想要根据需要加载图像,而不是一次加载所有图像。

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

对于作为应用程序打包器的一部分的图像, 这里可以提供一些见解。 如果要在应用程序运行期间存储图像,则可以使用相同的概念(用户使用图像的相对path)。

– 弗兰克

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