gpt4 book ai didi

iphone - 在 plist 中存储图像

转载 作者:太空狗 更新时间:2023-10-30 03:48:13 25 4
gpt4 key购买 nike

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

最佳答案

UIImage 没有直接实现存储在 plist 中所必需的 NSCoder 协议(protocol)。

但是,像下面这样添加是相当容易的。

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

注意:我不建议这样做,除非您真的想要,即对于非常小的图像。

关于iphone - 在 plist 中存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2486705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com