I am working on an application which performs some mathematical calculations and sends the results ( including texts and graphs) into a NSTextView.
This code works fine in making the output but this is the problem: I want that user can select/copy-pate and drag-drop the produced images into other text editors ( like Pages or Text Edit). When user drags an image from my textview, it is dragged but nothing is pasted to the destination:
- (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;
}
}