Objective-C / Cocoa:上传图像、工作内存和存储

发布于 2024-08-21 10:30:26 字数 441 浏览 2 评论 0原文

我正在将最初用 java 编写的应用程序移植到 cocoa 中,但我正在重写它以使其变得更好,因为无论如何我更喜欢 cocoa 。

我在应用程序中遇到的问题之一是,当您向其上传图像时,我创建了图像(例如 NSImage 对象),然后我将它们放在内存中,我上传的越多,它们占用的内存就越多占用了,最后我的内存耗尽了。

我的问题是:如果我要让用户在 cocoa 中将图像上传到这个应用程序,我应该如何存储它们?我不仅仅想复制文件路径,因为我希望保存的内容包含图像等。有没有办法上传图像并将其复制到仅适用于我的应用程序的不同位置?然后根据需要使用新路径名加载该图像?

只是我希望这一切能够得到巩固。我将通过将一个“主”对象归档到 NSData* 中来实现保存 - 所以我希望图像能够随之保存。

是否有一个临时位置可以将图像写入应用程序的磁盘,然后当我保存时,它们将全部存档到单个文件中?另外,我该怎么做?谢谢。

I'm in the process of porting an application originally in java to cocoa, but I'm rewriting it to make it much better, since I prefer cocoa a lot anyway.

One of the problems I had in the application, was that when you uploaded images to it, I had the images created, (as say an NSImage object) and then I just had them sitting in memory, the more I uploaded the more memory they took up, and I ended up running out of memory.

My question is this: if I am going to have users upload images to this application in cocoa, how should I go about storing them? I don't just want to copy the file paths, because I want what is saved to contain the images, etc. Is there any way to upload an image and copy it into a different place only for my application? Then load that image with the new path name as needed?

Only I would like it all to be consolidated. I'm going to implement saving by archiving one "master" object into an NSData*- so I'd like the images to be saved with that.

Is there a temporary location maybe where I could write the images to disk for my application, and then when I saved, they would all be archived into a single file? Also, how do I do this? Thanks.

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

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

发布评论

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

评论(1

波浪屿的海角声 2024-08-28 10:30:26

如果您只想临时存储图像,可以将图像存储在通过调用 NSTemporaryDirectory() 获得的临时文件夹中。这样,您就可以仅在需要显示图像时加载它们。

如果您想将图像与文档一起保存,那么您应该研究使用文档的包格式,以便文档实际上是包含图像和存档数据文件的文件夹。您可以使用 NSFileWrapper 的各种方法创建一个包含文档包的所有文件的文件包装器,然后您将实现 -fileWrapperOfType:error: 方法NSDocument 以处理保存。

这将允许您存储未更改的图像,然后在需要时从文档包中延迟加载它们。

If you only want to store the images temporarily, you can store the images in the temporary folder that you get by calling NSTemporaryDirectory(). You would then be able to load the images only when you need to display them.

If you want to save the images with your document then you should investigate using a package format for your document, so that the document is actually a folder containing your images and your archived data file. You can create a file wrapper containing all the files for your document bundle using the various methods of NSFileWrapper and then you would implement the -fileWrapperOfType:error: method of NSDocument in order to handle saving.

This would allow you to store the images unaltered and then lazily load them from the document bundle when required.

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