0

I am new to mac developing. I am developing an app about richEdit by subclassing NSTextView. In my customized textView, I can insert image like .bmp .jpeg .png. Code is like this:

NSTextAttachment* attachment;   
NSImage *image = [[NSImage alloc] initWithContentsOfFile:strRealPath];  //strRealPath is the absolute path of image;
attachment = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image];
[attachment setAttachmentCell:cell];
NSMutableAttributedString *attachmentString = (NSMutableAttributedString*)[NSMutableAttributedString attributedStringWithAttachment:attachment];
NSTextStorage* textStorage = [self textStorage];
[textStorage appendAttributedString:attachmentString];
[self didChangeText];      //self is my customized texview

Through this method, those image work well, but when the image is gif, the animation doesn't show. So, I tried this to display:

NSTextAttachment* attachment;   
NSImage *image = [[NSImage alloc] initWithContentsOfFile:strRealPath];  //strRealPath is the absolute path of image;
attachment = [[NSTextAttachment alloc] init];
NSTextAttachmentCell *cell = [[NSTextAttachmentCell alloc] initImageCell:image];
 NSMutableAttributedString *attachmentString = (NSMutableAttributedString*)[NSMutableAttributedString attributedStringWithAttachment:attachment];
NSTextStorage* textStorage = [self textStorage];
[textStorage appendAttributedString:attachmentString];
[self didChangeText];      //self is my customized texview

Finally,the gif animation works. But my problem comes: the right-click context menu doesn't show when I click or select the gif image, but other type image's context menu works well.

scarlett
  • 9
  • 2
  • Did you really manage to have an ANIMATED gif running in NSTextView? This posting suggests it might not be possible: http://stackoverflow.com/questions/29949348/animated-uiimage-in-uitextview-with-textkit And the second code you pasted above omits to set a cell on the attachment - was this really the fix? Are you able to confirm? Thanks. – Soferio Jan 10 '16 at 05:01

1 Answers1

0

i have figured it out by a category of NSImageView, it did work ,but i don't know why:

@interface NSImageView (NSImageView)

-(void)rightMouseDown:(NSEvent *)theEvent;

@end

@implementation NSImageView (NSImageView)

-(void)rightMouseDown:(NSEvent *)theEvent
{
[super rightMouseDown:theEvent];
}
scarlett
  • 9
  • 2