Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > [NSTextView replaceCharactersInRange: withRTFD:] problem

[NSTextView replaceCharactersInRange: withRTFD:] problem
Thread Tools
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Mar 22, 2001, 01:17 PM
 
I am trying to put images into a NSTextView programaticly, and am running into a silent failure. I am using PublicBeta, so if this is fixed for this weekend's release, that would be nice to know too.

Here is a quick example of what is not working for me:

//imageWindow is pointer to a NSTextView defined using InterfaceBuilder
NSData *thisImage;
int dataLength;

thisImage = [NSData dataWithContentsOfFile:@"/Developer/Examples/Carbon/SimpleText/Sources/SimpleTextDoc.tiff"];
dataLength = [thisImage length];

//this works
[imageWindow setIntValue:dataLength];
[imageWindow replaceCharactersInRange: NSMakeRange(0, 0) withString: @"thisWorked"];

//this does not
[imageWindow replaceCharactersInRange: NSMakeRange(0, 0) withRTFD: thisImage];

Also, am I correct in noticing that you have to declare all of your temporary variables first thing in a method? Every time I try and mix them into code the compiler complains to me about it.

Karl Kuehn
larkost@softhome.net
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 22, 2001, 04:03 PM
 
thisImage = [NSData dataWithContentsOfFile:@"/Developer/Examples/Carbon/SimpleText/Sources/SimpleTextDoc.tiff"];
...
[imageWindow replaceCharactersInRange: NSMakeRange(0, 0) withRTFD: thisImage];


"RTFD" refers to the RTF format + graphics, which, in this context is represented as a serialized, flattened data stored in an NSData. So, an image is not an appropriate argument there.

The NSFileWrapper object represents an RTFD, or actually any file package, in memory. So, you can create an NSFileWrapper from your image, then flatten it, and pass it to the above method. Something like:

NSFileWrapper *wrapper = [[NSFileWrapper alloc] initFileWithContents:thisImage];
// (can also use initWithPathath, and bypass the data)
NSData *serialized = [wrapper serializedData];
[imageWindow replaceCharactersInRange: NSMakeRange(0, 0) withRTFD: serialized];
[wrapper release];


There are several other ways to do this as well.

Also, am I correct in noticing that you have to declare all of your temporary variables first thing in a method? Every time I try and mix them into code the compiler complains to me about it.

Yes, Obj-C is compatible with C in this regard.

One more thing:

//this works
[imageWindow setIntValue:dataLength];


What was your intent with this? The setIntValue: is not needed. In fact, I didn't think NSText / NSTextView responded to this.

Ali




[This message has been edited by ali (edited 03-22-2001).]
     
larkost  (op)
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Mar 22, 2001, 04:36 PM
 
Thanks, I was wondering how the NSTextView understood the difference between rich-text textual data, and images, but figured it just read the first part and stumbled over the data tags (hence only .tiff and one other art mentioned in the docs). I will use the new stuff tonight.... here's hoping.

The part with:
[imageWindow setIntValue:dataLength];

does work, but is totally undocumented. It is just like setStringValue and setFloatValue. They work without requiring a cast, I like them.. just hope they get documented so that they are suck in stone....
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:46 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2