在plist中存储图像 [英] Storing image in plist

查看:94
本文介绍了在plist中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can we store images in plist file. Where is this plist file stored? Can anyone give me an example? Answers will be greatly appreciated!

推荐答案

UIImage 未实施直接存储在plist中的NSCoder协议。

The UIImage doesn't implement the NSCoder protocol directly that is necessary for storage in plists.

但是,如下所示添加相当容易。

But, it is fairly easy to add like below.

#import <Foundation/Foundation.h>

@interface UIImage (MyExtensions)
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;
@end



UIImage + NSCoder.m



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

添加此项后将能够将UIImages存储在plist中,即

When this has been added you will be able to store UIImages in plist with 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];

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

这篇关于在plist中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆