简体   繁体   English

如何将自定义对象加载并保存到iOS和OSX?

[英]How to load & save custom object to iOS and OSX?

I have a custom object that's used in both an iOS app and an OSX app. 我有一个在iOS应用程序和OSX应用程序中都使用过的自定义对象。 It look like this: 看起来像这样:

@interface Preset : NSObject

#if TARGET_OS_IPHONE
@property (nonatomic, strong) UIImage *thumbnail;
@property (nonatomic, strong) UIImage *customThumbnail;
#else
@property (nonatomic, strong) NSImage *thumbnail;
@property (nonatomic, strong) NSImage *customThumbnail;
#endif

@property (nonatomic, strong) NSDictionary *parms;
@property (nonatomic, strong) NSString *name;
@end

I want the object to be able to be loaded and saved in both iOS and OSX wherever I created it. 我希望无论我在哪里创建对象,都可以在iOS和OSX中加载和保存该对象。

I was going to implement the NSCoding methods for simple loading and saving, but then it occurred to me that I wouldn't be able to create a Preset file on OSX and use it in iOS because of the differences between NSImage and UIImage. 我打算实现NSCoding方法来进行简单的加载和保存,但是由于NSImage和UIImage之间的差异,我想到我无法在OSX上创建Preset文件并在iOS中使用它。

I'm sure there's a simple way to do this, but I don't see it. 我敢肯定有一个简单的方法可以做到这一点,但是我看不到。

Anyone? 任何人?

Thanks! 谢谢!

Convert the UIImage or NSImage to NSData containing the PNG representation of the image. UIImageNSImage转换为包含图像的PNG表示形式的NSData Then use that NSData in your NSCoding setup. 然后在您的NSCoding设置中使用该NSData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM