我们如何将图像存储在 plist 文件中。这个plist文件存储在哪里?谁能给我举个例子吗?答案将不胜感激!
The 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];
注意:我不建议这样做,除非您确实想要这样做,即对于非常小的图像。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)