2016-12-01 12 views
1

이미지가 NSAttributedString으로 저장된 서식있는 텍스트를 읽는 Mac 응용 프로그램이 있습니다. 이미지를 포함한 HTML로 변환하고 싶습니다.NSAttributedString에서 NSTextAttachment의 이미지 이름을 얻는 방법

HTML을 제대로 가져 오면 첨부 된 이미지를 열거 할 수도 있지만 첨부 된 이미지의 실제 파일 이름을 가져올 수는 없습니다. 결과 HTML에 필요한 것이 필요합니다.

attrString.enumerateAttribute(NSAttachmentAttributeName, 
     inRange: NSMakeRange(0, attrString.length), 
     options: [], 
     usingBlock: {attachment, range, _ in 

     if let attachment = attachment as? NSTextAttachment, 
      let fileWrapper = attachment.fileWrapper, 
      let data = fileWrapper.regularFileContents 
     { 
      // This prints <NSTextAttachment: 0x7fe68d5304c0> "Untitled 2.jpg" 
      // But there's no API to get the name? 
      print(attachment.description) 
     } 
    }) 

그래서 내가 NSTextAttachment 인스턴스와 끝까지

하지만 확인할 방법 : 첨부 된 이미지를 통해 열거

<p class="p1"><img src="file:///Untitled%202.jpg" alt="Untitled 2.jpg"></p> 

:

let htmlData = try attrString.dataFromRange(NSMakeRange(0, attrString.length), 
documentAttributes: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]) 

가 생성됩니다

HTML을 얻기 실제 파일 이름 ("제목 없음 2.jpg"). NSFileWrapper.filenamenil을 반환하며 텍스트 첨부 파일에서 이름을 가져 오기위한 API가 표시되지 않습니다.

절망적 인 부분은 정보가 텍스트 첨부 파일에 있다는 것입니다. 디버그 설명을 인쇄하면 파일 이름이 표시됩니다.

<NSTextAttachment: 0x7fe68d5304c0> "Untitled 2.jpg" 
<NSTextAttachment: 0x7fe68d533d60> "Pasted Graphic.tiff" 

어떻게 파일 이름에 액세스 할 수 있습니까? (디버그 설명을 구문 분석하지 않고)

+0

'fileWrapper.preferredFilename'? – Willeke

답변

1

나중에 참조 할 수 있습니다 : (thanks @Willeke) 대신 fileWrapper.preferredFilename을 사용하십시오. 누가 두 가지 속성이 있다고 생각했을까요 ...