3

添付ファイル付きの属性付き文字列を NSPasteboard に配置するための解決策を探すのに、かなりの数日を費やしました。

添付ファイル付きの RTFD ファイルを読み取り、そのテキストと属性を変更してから、それを NSPasteboard に貼り付けて、他のアプリケーション (たとえば、Mail.app) で使用することができます。これは正常に動作します。しかし、私がやりたいのは、テキストのある時点で画像を追加することです. テキストを属性付き文字列として使用することはできますが、(属性付き文字列への添付ファイルとして) 画像を挿入しようとすると、画像が届きません (残りは届きます)。

RTFDにはいろいろなフレーバーがあるようで、私が必要だと思ったものが連載されています。FileWrappers を使用しても、NSPasteboard で宣言された型の多くのバリエーションを試しましたが、何か重要なものが欠けているに違いありません。何をしても添付ファイルが届かないようです。

奇妙なことに、画像が添付された RTFD ファイルを読み込んで修正し、pasteBoard に貼り付けると、元の添付ファイルは正常に機能します。新しい添付ファイルを追加しようとしても、作成されません。例として、RTFD ファイルの読み取り、作業、ペーストボードのロード、および結果のメールへの貼り付けがあります。すべての元のテキストと画像に加えて、新しく変更または追加されたテキストと属性が表示されますが、添付された画像は単に欠落しています。

コード例を次に示します。

いくつかのテキストで属性付きの文字列を作成し、添付の画像を追加してから、もう少しテキストを追加し、それを textView に表示し (すべて機能します)、ペーストボードをロードして textEdit または Mail に貼り付けます... 添付の画像がありません、残りは次のとおりです。

// get the image
NSImage *myImage = [[NSImage alloc] initWithData:  [window dataWithPDFInsideRect:[theImage frame]]];

// set the image as an attachment
NSTextAttachment     *myAttachment  = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *myAttachmentCell = [[NSTextAttachmentCell alloc] initImageCell:myImage];
[myAttachment setAttachmentCell:myAttachmentCell];

// put image inside attributed string
NSAttributedString *myImageString = [NSAttributedString attributedStringWithAttachment:myAttachment] ;


// make an attributes dictionary (simply makes text blue) as an example
NSDictionary *myAttributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSColor blueColor], NSForegroundColorAttributeName,
                                 nil];

// and add some beginning text
NSMutableAttributedString *theCombinedString = [[NSMutableAttributedString alloc]  initWithString:[NSString stringWithFormat:@"Here's an image we just grabbed: \n\n"]  attributes:myAttributesDict];


// now append our attached image 
[theCombinedString appendAttributedString:myImageString];


// and add some following text as an example
NSMutableAttributedString *endString = [[NSMutableAttributedString alloc]  initWithString:[NSString stringWithFormat:@"\n\n How about that!\n"]  attributes:myAttributesDict];


// and stick it all together
[theCombinedString appendAttributedString: endString];


// now display it in a textView to make sure we have something 
[[junkTextView textStorage] appendAttributedString: theCombinedString];



/// --- works just fine to here --- ///



// the following loads the pastboard, including the added text, but for some reason, leaves out the above attachment 

NSPasteboard *thePboard = [NSPasteboard generalPasteboard];
[thePboard clearContents];

NSAttributedString *theContents = [[NSAttributedString alloc] theCombinedString ];

[thePboard writeObjects:[NSArray arrayWithObject:theContents]];


// pasting into mail or textEdit shows the above before and after text, but not the image.  

何か案は?

NSData、シリアル化された NSFileWrapper、さまざまなペーストボード タイプの設定などを使用してみました。これまでのところ、何も機能していないようです。画像を TIFF データとして読み込むと、問題なく貼り付けられますが、既に添付ファイルがあるファイルからより大きな文字列に挿入するには、属性付き文字列としてそれが必要です。

これは私の最初の投稿ですので、フォーマットエラーを許してください.

4

1 に答える 1

7

ついに解決策を見つけました、そしてそれは結局ラッパーでした。興味のある人、ファイルから読み取った画像、またはアプリから取得した画像を操作する人のためのコードは次のとおりです。

// make a file wrapper
NSFileWrapper* wrapper =[[NSFileWrapper alloc] initRegularFileWithContents:[theImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1]];

// -must- have this.  used to save your pasted file in some apps
[wrapper setPreferredFilename:@"yourImage.tiff"];
//
NSAttributedString* imageString = [NSAttributedString attributedStringWithAttachment:[[NSTextAttachment alloc] initWithFileWrapper:wrapper]];

// then load pasteboard and paste wherever you wish.  You can get fancier using private 
// pasteboards and custom data types, of course.  This is just a simple example.
于 2012-03-19T19:23:30.997 に答える