私は、いくつかの数学的計算を実行し、結果 (テキストやグラフを含む) を NSTextView に送信するアプリケーションに取り組んでいます。
このコードは出力を作成する際に問題なく機能しますが、これが問題です。ユーザーが生成された画像を選択/コピーアンドペーストして、他のテキストエディター (ページやテキスト編集など) にドラッグアンドドロップできるようにしたいのです。ユーザーがテキストビューから画像をドラッグすると、ドラッグされますが、何も貼り付けられません:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSBezierPath* aPath=[NSBezierPath bezierPath];
NSImage* anImage = [[NSImage alloc] initWithSize:NSMakeSize(200.0, 200.0)];
[anImage lockFocus];
//some drawings:
NSColor* blue= [NSColor blueColor];
[blue setStroke];
aPath=[NSBezierPath bezierPath];
[aPath setLineWidth:20];
[aPath moveToPoint:NSMakePoint(20.0, 20.0)];
[aPath lineToPoint:NSMakePoint(100.0, 100.0)];
[aPath stroke];
[anImage unlockFocus];
//re-capture and change it to a bitmap
NSSize size = [anImage size];
[anImage lockFocus];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0,0,size.width,size.height)];
[anImage unlockFocus];
NSData* jpgData = [rep representationUsingType: NSJPEGFileType properties:nil ];
NSImage* newImage= [[NSImage alloc]initWithData:jpgData];
//append attrited string only once. BOOL isDrownYet in header
if (!isDrownYet) {
NSTextAttachmentCell *attachmentCell1 = [[NSTextAttachmentCell alloc] initImageCell:newImage];
NSTextAttachment *attachment1 = [[NSTextAttachment alloc] init];
[attachment1 setAttachmentCell: attachmentCell1];
NSMutableAttributedString *attributedString1 = (NSMutableAttributedString*)[ NSMutableAttributedString attributedStringWithAttachment: attachment1];
[self.textStorage appendAttributedString:attributedString1];
isDrownYet=YES;
}
}