如何在NSTextView中显示gif图像

时间:2012-08-27 12:07:26

标签: objective-c gif nstextview nsfilewrapper

我是mac开发的新手。我正在通过继承NSTextView开发一个关于richEdit的应用程序。在我自定义的textView中,我可以插入像.bmp .jpeg .png这样的图像。代码是这样的:

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

通过这种方法,这些图像效果很好,但是当图像是gif时,动画不会显示。所以,我试着这个显示:

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

最后,gif动画有效。但我的问题是:当我点击或选择gif图像时,右键单击上下文菜单不显示,但其他类型图像的上下文菜单效果很好。

1 个答案:

答案 0 :(得分:0)

我已经通过一个NSImageView类别想出来了,它确实有效,但我不知道为什么:

@interface NSImageView (NSImageView)

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

@end

@implementation NSImageView (NSImageView)

-(void)rightMouseDown:(NSEvent *)theEvent
{
[super rightMouseDown:theEvent];
}