【问题标题】:the image of NSTextAttachment is flippedNSTextAttachment 的图像被翻转
【发布时间】:2018-04-04 11:14:14
【问题描述】:
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
NSImage *image = [NSImage imageNamed:@"emotion"];;
attachment.image = image;
NSAttributedString *attributedString = [NSAttributedString  attributedStringWithAttachment: attachment];
[[_feedbackContent textStorage] appendAttributedString:attributedString];

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

【问题讨论】:

标签: objective-c nstextattachment


【解决方案1】:

NSTextAttachment 似乎使用 NSFileWrapper 文件名来获取 UTI,并且基于 UTI 具有不同的行为。

我能够改用 NSFileWrapper 来修复它:

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];

您也可以尝试将fileType 属性设置为kUTTypePNG 或其他图像类型以使其正常工作。

雷达://47170950

【讨论】:

    【解决方案2】:

    将图像分配给 NSTextAttachmentCell,而不是 NSTextAttachment。

    id <NSTextAttachmentCell> cell = [[NSTextAttachmentCell alloc] initImageCell:image];
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    [attachment setAttachmentCell:cell];
    

    【讨论】:

    • 谢谢,这个答案最适合我。我之前确实绘制了翻转的图像,但这会导致剪切和复制操作出现问题。这种方法的一个副作用是设置NSTextAttachmentbounds 属性不再调整图像大小。我必须直接更改NSImagesize
    【解决方案3】:

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

    textAttachment.image = [NSImage imageWithSize:image.size flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
        [image drawInRect:dstRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
        return YES;
    }];
    

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 2023-03-07
      • 1970-01-01
      • 2018-01-17
      • 2011-08-02
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      相关资源
      最近更新 更多