Skip to content

Commit

Permalink
[Feat] add support base64
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed Jan 14, 2024
1 parent 1dcb1c5 commit 231c2f6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ ZNSTextAttachment enables NSTextAttachment to download images from remote URLs,
```
File > Swift Packages > Add Package Dependency
Add https://github.com/ZhgChgLi/ZNSTextAttachment.git
Select "Up to Next Major" with "1.1.6"
Select "Up to Next Major" with "1.1.8"
```
or
```swift
...
dependencies: [
.package(url: "https://github.com/ZhgChgLi/ZNSTextAttachment.git", from: "1.1.6"),
.package(url: "https://github.com/ZhgChgLi/ZNSTextAttachment.git", from: "1.1.8"),
]
...
.target(
Expand All @@ -35,7 +35,7 @@ platform :ios, '12.0'
use_frameworks!

target 'MyApp' do
pod 'ZNSTextAttachment', '~> 1.1.6'
pod 'ZNSTextAttachment', '~> 1.1.8'
end
```

Expand Down
34 changes: 24 additions & 10 deletions Sources/ZNSTextAttachment/ZNSTextAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,33 @@ public class ZNSTextAttachment: NSTextAttachment {
self.isLoading = false
})
} else {
let urlSessionDataTask = URLSession.shared.dataTask(with: imageURL) { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription as Any)
return
}

if let regex = try? NSRegularExpression(pattern: #"^data:image/(jpeg|jpg|png);base64,\s*(([A-Za-z0-9+/]*={0,2})+)$"#),
let firstMatch = regex.firstMatch(in: imageURL.absoluteString, options: [], range: NSRange(location: 0, length: imageURL.absoluteString.count)),
firstMatch.range(at: 1).location != NSNotFound,
firstMatch.range(at: 2).location != NSNotFound,
let mimeTypeRange = Range(firstMatch.range(at: 1), in: imageURL.absoluteString),
let base64StringRange = Range(firstMatch.range(at: 2), in: imageURL.absoluteString),
let base64Data = Data(base64Encoded: String(imageURL.absoluteString[base64StringRange]), options: .ignoreUnknownCharacters) {

let mimeType = String(imageURL.absoluteString[mimeTypeRange])
self.dataDownloaded(base64Data, mimeType: mimeType)

self.dataDownloaded(data, mimeType: response?.mimeType)
self.isLoading = false
self.urlSessionDataTask = nil
} else {
let urlSessionDataTask = URLSession.shared.dataTask(with: imageURL) { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription as Any)
return
}


self.dataDownloaded(data, mimeType: response?.mimeType)
self.isLoading = false
self.urlSessionDataTask = nil
}
self.urlSessionDataTask = urlSessionDataTask
urlSessionDataTask.resume()
}
self.urlSessionDataTask = urlSessionDataTask
urlSessionDataTask.resume()
}
}

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/ZNSTextAttachment.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ZNSTextAttachment"
s.version = "1.1.7"
s.version = "1.1.8"
s.summary = "ZNSTextAttachment enables NSTextAttachment to download images from remote URLs."
s.homepage = "https://github.com/ZhgChgLi/ZNSTextAttachment"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down

0 comments on commit 231c2f6

Please sign in to comment.