在UIImage不直接实现NSCoder协议是必要的存储在Plist档案.
但是,如下所示添加相当容易.
#import <Foundation/Foundation.h>
@interface UIImage (MyExtensions)
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;
@end
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
添加完成后,您就可以将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];
Run Code Online (Sandbox Code Playgroud)
注意:我不建议这样做,除非你真的想要,即对于非常小的图像.
| 归档时间: |
|
| 查看次数: |
9855 次 |
| 最近记录: |