@@ -70,39 +70,42 @@ public class AsyncTextAttachment: NSTextAttachment {
7070 // MARK: - Helpers
7171
7272 private func startAsyncImageDownload( ) {
73- guard let imageURL = imageURL, contents == nil else {
73+ guard let imageURL = imageURL else {
7474 return
7575 }
7676
7777 DispatchQueue . global ( qos: . background) . async {
78- if let image = SDImageCache . shared . imageFromCache ( forKey : imageURL. absoluteString ) {
78+ if let image = self . getCacheImage ( with : imageURL) {
7979 self . display ( image, with: image. pngData ( ) , url: imageURL)
8080 } else {
8181 self . downloadImage ( with: imageURL)
8282 }
8383 }
8484 }
8585
86+ private func getCacheImage( with: URL ) -> UIImage ? {
87+ return SDImageCache . shared. imageFromCache ( forKey: with. absoluteString)
88+ }
89+
8690 private func downloadImage( with: URL ) {
8791 SDWebImageDownloader . shared. downloadImage ( with: with, options: [ . decodeFirstFrameOnly] , progress: nil ) { ( image, data, _, _) in
88- self . display ( image, with: data, url: with)
92+ let roundedImage = image? . circleCorners ( finalSize: self . bounds. size)
93+ DispatchQueue . global ( qos: . background) . async {
94+ SDImageCache . shared. storeImage ( toMemory: roundedImage, forKey: with. absoluteString)
95+ }
96+
97+ self . display ( roundedImage, with: data, url: with)
8998 }
9099 }
91100
92101 public func display( _ image: UIImage ? , with: Data ? , url: URL ) {
93- self . contents = with
94-
95- DispatchQueue . global ( qos: . background) . async {
96- SDImageCache . shared. storeImage ( toMemory: image, forKey: url. absoluteString)
97- }
98102 let ext = url. pathExtension as CFString
99103 if let uti = UTTypeCreatePreferredIdentifierForTag ( kUTTagClassFilenameExtension, ext, nil ) {
100104 self . fileType = uti. takeRetainedValue ( ) as String
101105 }
102- if let image = image? . circleCorners ( finalSize : self . bounds . size ) { //2 was causing weird clipping
106+ if let image = image { //2 was causing weird clipping
103107 let imageSize = image. size
104108
105- self . contents = image. pngData ( )
106109 self . originalImageSize = imageSize
107110 }
108111
@@ -116,7 +119,7 @@ public class AsyncTextAttachment: NSTextAttachment {
116119 public override func image( forBounds imageBounds: CGRect , textContainer: NSTextContainer ? , characterIndex charIndex: Int ) -> UIImage ? {
117120 if let image = image { return image }
118121
119- guard let contents = contents , let image = UIImage ( data : contents ) else {
122+ guard let url = imageURL , let image = getCacheImage ( with : url ) else {
120123 // remember reference so that we can update it later
121124 self . textContainer = textContainer
122125
0 commit comments