2012-08-27 105 views
0

我是mac開發新手。我正在通過繼承NSTextView來開發一個關於richEdit的應用程序。在我自定義的textView中,我可以插入像.bmp .jpeg .png這樣的圖像。代碼是這樣的:如何在NSTextView中顯示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]; 
[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圖像,但其他類型圖像的上下文菜單運行良好。

+0

你真的設法在NSTextView運行GIF動畫?這貼子表明它可能是不可能的:http://stackoverflow.com/questions/29949348/animated-uiimage-in-uitextview-with-textkit上面粘貼的第二個代碼省略了在附件上設置單元格 - 是這個真的是修復?你能確認嗎?謝謝。 – Soferio

回答

0

我有NSImageView的類別理解了它,它沒有工作,但我不知道爲什麼:

@interface NSImageView (NSImageView) 

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

@end 

@implementation NSImageView (NSImageView) 

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