Re: Creating a string with attachements from thin air... and an image...
Re: Creating a string with attachements from thin air... and an image...
- Subject: Re: Creating a string with attachements from thin air... and an image...
- From: Renaud Boisjoly <email@hidden>
- Date: Fri, 28 Mar 2003 13:29:45 -0500
After diggin even further in this list, I found some sample code from
Ben Haller which helped. He was putting a proper jpeg or other format
file on the pasteboard but creating a filewrapper before, and this is
what his code was:
NSFileWrapper *wrap = [[NSFileWrapper alloc]
initRegularFileWithContents:data];
NSMutableDictionary *wrapDict = [NSMutableDictionary dictionary];
NSTextAttachment *attachment;
NSAttributedString *attrStr;
NSData *rtfData;
[wrapDict setObject:[NSNumber
numberWithUnsignedLong:FOUR_CHAR_CODE('SSct')]
forKey:NSFileHFSCreatorCode];
[wrapDict setObject:[NSNumber
numberWithUnsignedLong:FOUR_CHAR_CODE('JPEG')]
forKey:NSFileHFSTypeCode];
[wrapDict setObject:NSFileTypeRegular forKey:NSFileType];
[wrapDict setObject:[[wrap fileAttributes]
objectForKey:NSFilePosixPermissions] forKey:NSFilePosixPermissions];
[wrap setFileAttributes:wrapDict];
[wrap setPreferredFilename:@"clipboard.jpg"];
[pasteboard writeFileWrapper:wrap];
attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrap];
attrStr = [NSAttributedString
attributedStringWithAttachment:attachment];
rtfData = [attrStr RTFDFromRange:NSMakeRange(0, [attrStr length])
documentAttributes:nil];
[pasteboard addTypes:[NSArray arrayWithObjects:NSRTFDPboardType,
nil] owner:nil];
[pasteboard set
Data:rtfData forType:NSRTFDPboardType];
[wrap release];
I basically removed the pasteboard from the equation and set my string
to the attrStr and it now works beautifully!
Thanks Ben!
-Arby
On Friday, Mar 28, 2003, at 09:11 America/Montreal, Renaud Boisjoly
wrote:
Hi!
I'm trying to import PICT image files into a textView and I'm facing
strange issues... here is my documented code:
myData = [thisImporter dataForType:'PICT' Id:1];
NSImage *newImage = [[NSImage alloc] initWithData:myData];
NSLog(@"PICT image: %@",newImage); //Things look right at this point...
id myWrapper = [[NSFileWrapper alloc] initWithPath:[[NSBundle
mainBundle] pathForResource:@"new" ofType:@"tiff"]];
//I'm using a default filewrapper, I haven't figured-out how to create
an NSTextAttachement completely programmatically, perhaps this is my
issue?
id myAttachment = [[NSTextAttachment alloc] initWithFileWrapper:
myWrapper];
NSTextAttachmentCell *myCell = [[NSTextAttachmentCell alloc]
initImageCell:newImage];
[happyAttachment setAttachmentCell:myCell];
NSAttributedString *myString = [NSAttributedString
attributedStringWithAttachment: myAttachment] ;
NSLog(@"Text: %@",myString);
[[[myTextView textStorage] appendAttributedString:myString ];
All I get in the final TextView is a blank. If I do not try to
substitute the NSTextAttachementCell, I still get the original graphic
(new.tiff).
I also tried programmatically, without a default FileWrapper, but I
would still only get blanks... I was using something like:
NSTextAttachment *myAttachement = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *myCell = [[NSTextAttachmentCell alloc]
initImageCell:newImage];
[myAttachement setAttachmentCell:myCell];
NSAttributedString *myString = [NSAttributedString
attributedStringWithAttachment:happyAttachment] ;
But that too doesn't work
I'm sure I'm missing something trivial... any clues ?
Arby
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.