2014-12-07 2 views
1

setImportGraphics(true) NSTextView 가지고, 거기에 이미지를 끌 수 있습니다, 그들은 인터페이스에 표시하지만 프로그래밍 방식으로 이미지를 가져 오는 방법 및 아이디어를 가지고 일단 그것을 끌고있다.어디에 NSTextView 끌기 이미지 참조

내가 myNSTextView.string이라고 부르는 경우 이미지 주위에 텍스트가 있지만 이미지가 존재하지 않는 것 같습니다.

이 경우를 관리하려면 끌어서 놓기와 관련하여 몇 가지 방법을 구현해야합니까?

답변

0

이미지를 프로그래밍 방식으로 가져 와서 저장 한 후 그 이미지를 드래그하면 어떻게되는지 알 수 없습니다.

삭제 된 이미지는 NSTextAttachment로 NSTextStorage에 추가됩니다. 따라서 삭제 된 이미지에 액세스하려면 textStorage의 내용을 반복하고 이미지 파일을 준수하는 첨부 파일이 있는지 확인해야합니다.

내가이 경우

이 길을 갈하려는 경우 당신은 확실히를 NSTextView을 확장하고 - (void)performDragOperation:(id<NSDraggingOperation>)sender 방법을 덮어에 의해 삭제 된 파일을 처리 할 수 ​​있습니다를 관리하는 드래그 앤 드롭에 관한 몇 가지 방법을 구현해야합니까 Apple의 Drag and Drop Programming Topics 문서를 읽어 보시기 바랍니다.

서브 클래 싱의 팬이 아니기 때문에이 문제에 대한 대답은 NSAttributedString 범주를 사용하여 연결된 이미지의 NSArray를 반환합니다. 어느 아래의 코드로 해결 될 수있다 : 당신이 GIT를 사용하는 경우

#import "NSAttributedString+AttachedImages.h" 

@implementation NSAttributedString (AttachedImages) 


- (NSArray *)images 
{ 
    NSMutableArray *images = [NSMutableArray array]; 
    NSRange effectiveRange = NSMakeRange(0, 0); 

    NSTextAttachment *attachment; 
    CFStringRef extension; 
    CFStringRef fileUTI; 

    while (NSMaxRange(effectiveRange) < self.length) { 
     attachment = [self attribute:NSAttachmentAttributeName atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange]; 

     if (attachment) { 
      extension = (__bridge CFStringRef) attachment.fileWrapper.preferredFilename.pathExtension; 
      fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL); 
      if (UTTypeConformsTo(fileUTI, kUTTypeImage)) { 
       NSImage *theImage = [[NSImage alloc] initWithData:attachment.fileWrapper.regularFileContents]; 
       [theImage setName:attachment.fileWrapper.preferredFilename]; 

       [images addObject:theImage]; 
      } 
     } 
    } 

    return images.copy; 
} 

@end 

, 내 Github repository의 코드를 복제 할 수 있습니다.

도움이 되었기를 바랍니다.