Hello,

I'm updating some code in which images get added to text as symbolic links, and 
I'm a little confused about some differences I am seeing in code I thought 
should act the same.

Specifically, in 10.6, NSFileWrapper's -initSymbolicLinkWithDestination: method 
has been replaced with -iniSymbolicLinkWithDestinationURL:. I use this method 
to add an image to text as a symbolic link along these lines:

NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initSymbolicLinkWithDestination:@"/users/mypath/myimg.jpg"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
NSAttributedString *aStr = [NSAttributedString 
attributedStringWithAttachment:attachment];
[[textView textStorage] replaceCharactersInRange:imgRange 
withAttributedString:aStr];
[attachment release];
[wrapper release];

This result of this is that you end up with an image in the text, and if you 
double-click on the image it opens the image from file in its default editor - 
exactly the same as if you held down the Control key while dropping an image 
into TextEdit, for instance.

So, I assumed that just replacing the deprecated method with 
-initSymbolicLinkWithDestinationURL: would do the same thing, given that the 
only difference is that one takes an NSString and the other takes an NSURL:

NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initSymbolicLinkWithDestinationURL:[NSURL 
fileURLWithPath:@"/users/mypath/myimg.jpg"]];
// ...rest of the code the same.

However, it doesn't. The image doesn't get set automatically - instead you see 
the default symbolic link icon (a blank document icon) where the image appears 
if you use the deprecated file wrapper method instead.

(Note that I've checked the created NSURL exactly matches the 
-symbolicLinkDestinationURL returned when creating the wrapper using the 
deprecated method, so it doesn't seem to be a problem with the URL itself.)

It's easy enough to set the image myself, of course, with a few extra lines of 
code:

NSURL *fileURL = [NSURL fileURLWithPath:@"/users/mypath/myimg.jpg"];
NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initSymbolicLinkWithDestinationURL:fileURL];
NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];

// Add the image explicitly given that NSTextAttachment doesn't do it 
automatically in this instance.
NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL];
[(NSTextAttachmentCell *)[attachment attachmentCell] setImage:image];
[image release];

NSAttributedString *aStr = [NSAttributedString 
attributedStringWithAttachment:attachment];
[[textView textStorage] replaceCharactersInRange:NSMakeRange(0,0) 
withAttributedString:aStr];
[attachment release];
[wrapper release];

But my understanding from the NSTextAttachment docs is that this should be 
automatic:

--
Discussion
This method is the designated initializer for the NSTextAttachment class.
If aWrapper contains an image file that the receiver can interpret as an 
NSImage object, sets the attachment cell’s image to the NSImage rather than to 
the icon of aWrapper.
--

My guess is that this is a bug in NSTextAttachment - that NSTextAttachment 
hasn't been updated to grab the image from a destination URL; but then, this 
doesn't make sense as NSLogging reveals that 
-initSymbolicLinkWithDestinationURL: sets the NSString -symbolicLinkDestination 
as well as the URL form.

So, before I report this as a possible bug:
1) Am I making a bad assumption - should I have always been setting the 
NSTextAttachment cell image manually anyway?
2) Am I making some other mistake in the code above that would cause the 
difference between using -initSymbolicLinkWithDestination: and 
-initSymbolicLinkWithDestinationURL:

Not a big deal, obviously, as it's easy to fix with three lines of code, I'm 
just curious as to why there is a difference in case I'm missing something.

Thanks and all the best,
Keith



_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to