2014-09-10 1 views
1

신속한 자격 상한을위한 lib를 개발 중이며 잘 작동합니다! 하지만 약간 문제가있다,이 같은 원본 이미지를로드하기 위해 내가 자리 표시 자 이미지 넣어 인하에 있고 다른 프로세스를 파견 이미지를로드 할 때 :UITextView에서 URL이 placeholder로 표시된 이미지가 속성 문자열로 표시됩니다.

var attachment: NSTextAttachment = NSTextAttachment() 

      attachment.image = UIImage(named: "placeholder.png") 
      attachment.bounds = CGRect(origin: CGPointZero, size: attachment.image.size.width > UIScreen.mainScreen().bounds.width ? CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size) 

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in 
       let imageData: NSData = NSData(contentsOfURL: url) 

       attachment.image = UIImage(data: imageData) 
       attachment.bounds = CGRect(origin: CGPointZero, size: attachment.image.size.width > UIScreen.mainScreen().bounds.width ? CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size) 
       self.textView.setNeedsDisplay() 
      }) 

      let attributedString: NSAttributedString = NSAttributedString(attachment: attachment) 
      let lineBreak: NSAttributedString = NSAttributedString(string: "\n") 

      self.markdown.deleteCharactersInRange(matchRange) 

      self.markdown.insertAttributedString(attributedString, atIndex: matchRange.location) 
      self.markdown.insertAttributedString(lineBreak, atIndex: matchRange.location) 

하지만 사용자 때까지 사라지지 않는 이미지 장소 홀더를로드 한 후 스크롤을 시작, 갑자기 이미지를 수정 표시!

그래서 나는 무엇이 문제인지 알고 싶습니다.

답변

0

UI를 글로벌 큐에서 변경하려고하면 UI가 작동하지 않을 수 있습니다. 이런 경우 메인 큐를 사용해야 할 수도 있습니다.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in 
    let imageData: NSData = NSData(contentsOfURL: url) 
    let image = UIImage(data: imageData) 

    dispatch_async(dispatch_get_main_queue()) { 
     // On the main queue (the main thread) 
     attachment.image = image 
     attachment.bounds = CGRect(origin: CGPointZero, size: attachment.image.size.width > UIScreen.mainScreen().bounds.width ? CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.width/attachment.image.size.width * attachment.image.size.height) : attachment.image.size) 
     self.textView.setNeedsDisplay() 
    } 
}) 
+0

작동하지 않습니다! 자리 표시 자 변경을 원본 이미지로 스크롤하기 시작할 때와 이전과 동일합니다. 다른 아이디어있어? – Aryan

+0

NSTextStorage를 호출하는 것은 어떻습니까? -edited : range : changeInLength : self.textView 용 메서드 textStorage 객체? –

+0

범위를 사용할 수 없습니다. 다른 스레드가 markdown 텍스트에서 작업 중이기 때문에 이미지 링크를 찾아서 제거하고 이미지를 삽입하여 다른 범위가 잘못 될 수 있습니다! – Aryan