Objective c NSTextAttachment的图像已翻转

Objective c NSTextAttachment的图像已翻转,objective-c,nstextattachment,Objective C,Nstextattachment,将图像添加到NSTextAttachment后,图像将垂直翻转。任何人都知道如何解决这个问题。NSTextAttachment似乎使用NSFileWrapper文件名来获取UTI,并且根据UTI有不同的行为 我可以改为使用NSFileWrapper来修复它: NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; NSImage *image = [NSImage imageNamed:@"emotion"];; attac

将图像添加到NSTextAttachment后,图像将垂直翻转。任何人都知道如何解决这个问题。

NSTextAttachment似乎使用NSFileWrapper文件名来获取UTI,并且根据UTI有不同的行为

我可以改为使用NSFileWrapper来修复它:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
NSImage *image = [NSImage imageNamed:@"emotion"];;
attachment.image = image;
NSAttributedString *attributedString = [NSAttributedString  attributedStringWithAttachment: attachment];
[[_feedbackContent textStorage] appendAttributedString:attributedString];
您还可以尝试将
fileType
属性设置为
kUTTypePNG
或其他图像类型以使其正常工作


radar://47170950

在绘制翻转视图时(这就是图像被翻转的原因),我找到的唯一解决方法是创建翻转图像,然后将其绘制为:

NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
// Without a filename (which is used to get the UTI by NSTextAttachment)
// the image is displayed flipped.
[fileWrapper setPreferredFilename:@"Attachment.png"];
NSTextAttachment  *attachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];

将映像分配给NSTextAttachmentCell,而不是NSTextAttachment

textAttachment.image = [NSImage imageWithSize:image.size flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
    [image drawInRect:dstRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    return YES;
}];
id cell=[[NSTextAttachmentCell alloc]initImageCell:image];
NSTextAttachment*attachment=[[NSTextAttachment alloc]initWithData:nil of type:nil];
[附件setAttachmentCell:cell];

replice of?可能的replice of?您找到解决方案了吗?谢谢,这个答案最适合我。我以前确实画过翻转的图像,但这会导致剪切和复制操作出现问题。这种方法的一个副作用是设置
NSTextAttachment
bounds
属性不再调整图像大小。我必须直接更改
NSImage
大小。
id <NSTextAttachmentCell> cell = [[NSTextAttachmentCell alloc] initImageCell:image];

NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
[attachment setAttachmentCell:cell];