Saving image in plist

How to save images in a plist file. Where is this plist file stored? Can someone give me an example? Answers will be greatly appreciated!

+5
source share
4 answers

UIImage does not directly implement the NSCoder protocol, which is required for storage in plists.

But it is quite easy to add, as shown below.

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

When this is added, you will be able to store UIImages in a plist, i.e.

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

Note. I do not recommend doing this if you really do not want it, that is, for really small images.

+8
source

.plist, . , . , .

+4

, .

For images as part of the application, here may give some idea. If you want to store images during application launch , you can use the same concepts (user layer with relative image paths).

- Frank

+1
source

Plist files are for storing key-value pairs, not your application’s resources. Icons, images, and forms are usually stored in a .xib file (see "Interface Builder")

0
source

All Articles