如何输出视图层次结构&内容到文件? [英] How to output a view hierarchy & contents to file?

查看:183
本文介绍了如何输出视图层次结构&内容到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个美化的绘画应用程序,用户可以在其中粘贴图像,绘制一束UIBezier曲线,并移动所有这些东西并旋转它们。



大部分是通过将UIImageViews和(子类)UIViews作为子视图堆叠到视图控制器的主视图中实现的。



我想让用户能够将他们创建的状态保存为某种文件,并与其他人共享(例如通过DropBox)。



保存本身不是问题(我知道如何保存文件!),只是'最好的'方式从应用程序获取所有的信息到文件s),然后将其加载回来。



我猜想已经有成熟的框架,这样做,但奇怪的是,没有找到任何地址这个。



因此,在我开始编写自己的自定义方法来做所有这些之前,我想我会问:



是否有一些推荐,批准(甚至是简单; - ))实现保存 - 如写入文件 - 给定UIView的所有子视图的状态?



您对我的尝试的建议?谢谢。

解决方案

tl; dr




NSKeyedArchiver.archiveRootObject(view,toFile:/ path / to / archive)




let view = NSKeyedUnarchiver.unarchiveObjectWithFile(/ path / to / archive)









Bingo! NSKeyedArchiver



让我们使用这个类将UIView序列化为一个文件,其中 archiveRootObject:toFile: code> 。然后,我们将从 unarchiveObjectWithFile:



正在保存到文件:
NSKeyedArchiver.archiveRootObject(view,toFile:/ path / to / archive)



从文件读取
let view = NSKeyedUnarchiver.unarchiveObjectWithFile(/ path / to / archive)


I'm writing a glorified 'paint' application where a user can paste in images, and draw a bunch of UIBezier curves, and move all these things around and rotate them.

Most of these are implemented by stacking UIImageViews and (sub-classed) UIViews as subviews onto the main view in the view controller.

I want the user to be able to save the state of their creation as some kind of file and share it with others (e.g. via DropBox).

The saving itself isn't the issue (I know how to save files!), just the 'best' way to get all the information from the app into file(s) and then load it back in.

I'm guessing there are already well-established frameworks for doing exactly this, but oddly, haven't found anything that addresses this.

So, before I set about writing my own custom methods for doing all this, I thought I'd ask:

Is there some recommended, approved (and maybe even 'easy' ;-)) way to implement saving -- as in writing to files -- the state of all the subviews of a given UIView?

Your recommendations for what I'm "trying to do"? Thanks.

解决方案

tl;dr

Going Down:
NSKeyedArchiver.archiveRootObject(view, toFile: "/path/to/archive")

Coming Up:
let view = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive")

)

UIView conforms to NSCoding

NSCoding enables the creation of archives, which

provide a means to convert objects and values into an architecture-independent stream of bytes.

Now we have all the pieces to the puzzle, let's put them together:

We know we're going to need an archiver, so let's search the docs until we find something promising:

Bingo! NSKeyedArchiver

Let's use this class to serialize a UIView to a file with archiveRootObject:toFile:. Then we'll deserialize it from a file with unarchiveObjectWithFile:.

Saving to file:
NSKeyedArchiver.archiveRootObject(view, toFile: "/path/to/archive")

Reading from file:
let view = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/archive")

这篇关于如何输出视图层次结构&内容到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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