如何在 NSCoder 中编码 UIImage

发布于 2024-12-13 12:33:49 字数 662 浏览 3 评论 0原文

我有一个“User”对象,其中包含“UIImage”类的属性。

如何将图像与其他属性一起编码并将其保存在 NSUserDefaults 中?

我的理解是我只能编码 NSString

例如,当前在我的代码中,我在编码/解码实现中添加 avatar_url (字符串)。如何将 url 转换为 UIImage 然后对其进行编码?

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.avatar_url forKey:@"avatar_url"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil ){
         self.uid = [decoder decodeObjectForKey:@"uid"];
         self.avatar_url = [decoder decodeObjectForKey:@"avatar_url"];
    }
}

I have a 'User' object that contains a property of 'UIImage' class.

How do I encode the image together with the other properties and save it in NSUserDefaults?

My understanding is that I can only encode NSStrings?

For example, currently in my code, I am adding the avatar_url (string) in the encode/decode implementation. How can I convert the url to UIImage and then encode it?

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.avatar_url forKey:@"avatar_url"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil ){
         self.uid = [decoder decodeObjectForKey:@"uid"];
         self.avatar_url = [decoder decodeObjectForKey:@"avatar_url"];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-12-20 12:33:49

更新

正如评论中指出的,UIImage 符合 NSCoding,因此您可以直接对其进行编码,如下所示:

[encoder encodeObject:image forKey:@"image"];

这样做的优点是它(应该)存储所有图像的属性,包括 scaleimageOrientation,如果未将它们设置为默认值,则保留这些属性可能非常重要。

但是,它确实存在一些您应该注意的风险。如果图像是直接从您的应用程序包加载的,则存档将包含图像名称,但图像数据。如果存档确实包含图像数据,则它将采用 PNG 格式,而您可能更喜欢 JPEG 格式,具体取决于您要编码的图像类型。

原始

使用 UIImagePNGRepresentationUIImageJPEGRepresentationUIImage 转换为 NSData,然后对 NSData 进行编码code>:

[encoder encodeObject:UIImagePNGRepresentation(self.image) forKey:@"image"];

稍后解码图像:

self.image = [UIImage imageWithData:[decoder decodeObjectForKey:@"image"]];

UPDATE

As pointed out in the comments, UIImage conforms to NSCoding, so you can just encode it directly, like this:

[encoder encodeObject:image forKey:@"image"];

This has the advantage that it (should) store all the properties of the image, including scale and imageOrientation, which may be pretty important to preserve if they're not set to the defaults.

However, it does have some risks you should be aware of. If the image was loaded directly from your app bundle, the archive will contain the image name, but not the image data. And if the archive does contain the image data, it will be in PNG format, while you might prefer JPEG format depending on the kind of image you're encoding.

ORIGINAL

Use UIImagePNGRepresentation or UIImageJPEGRepresentation to convert the UIImage to an NSData, then encode the NSData:

[encoder encodeObject:UIImagePNGRepresentation(self.image) forKey:@"image"];

To decode the image later:

self.image = [UIImage imageWithData:[decoder decodeObjectForKey:@"image"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文