如何从NSAttributedString获取图像数据

时间:2014-04-20 18:34:00

标签: cocoa nsattributedstring

我有一个NSTextView。我将图像粘贴到其中并查看。当我获得文本视图的NSAttributedString的NSTextAttachment时,它的文件包装器是nil。如何获取粘贴到文本视图中的图像数据?

我在NSAttributedString上使用类别来获取文本附件。如果可能的话,我宁愿不写入磁盘。

- (NSArray *)allAttachments
{
    NSError *error = NULL;
    NSMutableArray *theAttachments = [NSMutableArray array];
    NSRange theStringRange = NSMakeRange(0, [self length]);
    if (theStringRange.length > 0)
    {
        NSUInteger N = 0;
        do
        {
            NSRange theEffectiveRange;
            NSDictionary *theAttributes = [self attributesAtIndex:N longestEffectiveRange:&theEffectiveRange inRange:theStringRange];
            NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
            if (theAttachment != NULL){
                NSLog(@"filewrapper: %@", theAttachment.fileWrapper);
                [theAttachments addObject:theAttachment];
            }
            N = theEffectiveRange.location + theEffectiveRange.length;
        }
        while (N < theStringRange.length);
    }
    return(theAttachments);
}

3 个答案:

答案 0 :(得分:8)

  1. 枚举附件。 [NSTextStorage enumerateAttribute:...]
  2. 获取附件的filewrapper。
  3. 写入网址。

    [textStorage enumerateAttribute:NSAttachmentAttributeName
                            inRange:NSMakeRange(0, textStorage.length)
                            options:0
                         usingBlock:^(id value, NSRange range, BOOL *stop)
     {
         NSTextAttachment* attachment = (NSTextAttachment*)value;
         NSFileWrapper* attachmentWrapper = attachment.fileWrapper;
         [attachmentWrapper writeToURL:outputURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil];
         (*stop) = YES; // stop so we only write the first attachment
     }];
    
  4. 此示例代码仅将第一个附件写入outputURL。

答案 1 :(得分:3)

您可以从附件单元中获取包含的NSImage。

简约示例:

// assuming we have a NSTextStorage* textStorage object ready to go, 
// and that we know it contains an attachment at some_index
// (in real code we would probably enumerate attachments).

NSRange range;
NSDictionary* textStorageAttrDict = [textStorage attributesAtIndex:some_index
                                             longestEffectiveRange:&range 
                                                           inRange:NSMakeRange(0,textStorage.length)];

NSTextAttachment* textAttachment = [textStorageAttributesDictionary objectForKey:@"NSAttachment"];
NSTextAttachmentCell* textAttachmentCell = textAttachment.attachmentCell;
NSImage* attachmentImage = textAttachmentCell.image;

EDITING: 仅限OS X(AppKit版本)

答案 2 :(得分:0)

@EmeraldWeapon的answer对Objective-C很有用,但在Swift中却有所下降,因为在Swift中attachmentCell不是NSTextAttachmentCell,而是NSTextAttachmentCellProtocol?不提供.image)-因此,在访问.image之前,您需要将其强制转换为具体实例:

func firstImage(textStorage: NSTextStorage) -> NSImage? {
    for idx in 0 ..< textStorage.string.count {
        if
            let attr = textStorage.attribute(NSAttributedString.Key.attachment, at: idx, effectiveRange: nil),
            let attachment = attr as? NSTextAttachment,
            let cell = attachment.attachmentCell as? NSTextAttachmentCell,
            let image = cell.image {

            return image
        }
    }
    return nil
}