Graphic image saving in text editor?
I am making a basic text editor from this tutorial here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextArchitecture/Tasks/TextEditor.html
My text editor can save, write, and open doc开发者_开发知识库uments in RTF and TXT format, but cannot save graphics along with any text. Formatted text is saved, and the graphic does display when the window is open, but does not get saved.
You're almost there, you just need to implement a bit more in your app's Info.plist.
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
NSData *data;
[self setString:[textView textStorage]];
NSMutableDictionary *dict =
[NSDictionary dictionaryWithObject:NSRTFTextDocumentType
forKey:NSDocumentTypeDocumentAttribute];
Notice that in the method which asks for the data to be saved to file, no matter what, it's set up to use NSRTFTextDocumentType rather than NSRTF*D*TextDocumentType. RTFD means RTF with attachments, which saves an .rtfd document which is actually a package/bundle (a folder that is presented to the user as if it were a single file). Saving as NSRTFTextDocumentType will effectively discard the stuff that can't fit into an RTF document, like the images.
After step 12 is the following:
At this stage of its development, your editor opens and saves documents only with an extension of ????. To enable your application to save and open documents with a recognized file type, you need to use Xcode to configure the document types settings in the application’s property list file in the Resources folder in Xcode. (The Xcode template names the file with your project name followed by -Info.plist.) You can edit this file in Xcode by selecting the file in the Groups & Files list and using the built-in editor. Click the disclosure triangles to edit the value of the first item under CFBundleTypeExtensions to the preferred extension for your document files.
For more information about property list files, see “Storing Document Types Information in the Application's Property List” in Document-Based Applications Overview. For complete details about application property lists, see Runtime Configuration Guidelines.
Basically, right now, you're being passed in a generic DocumentType in that data method shown above. Once you claim in your Info.plist that you can handle RTF and RTFD data (as 2 separate entries), you will be passed in a different value in that method, depending on what the user has chosen in the Format popup button in the NSSavePanel. You can check the value of the passed in type and then specify NSRTFTextDocumentType or NSRTFDTextDocumentType accordingly.
You can probably look at TextEdit's Info.plist to use as the basis of your Document types, though be sure to change the NSDocument subclass name to your own so the NSDocumentController knows what class to use.
maybe your images are not saved in the same directory?
for example if I create a rtf document with TextEdit it creates an folder called something.rtfd
and contains the TXT.rtf
and all the images.
精彩评论