Skip to content

Commit 355712f

Browse files
authored
Merge pull request #17 from Jake-Short/fix-url-image-dependency
Fix url image dependency (2.0.0 update)
2 parents c13c0e4 + 24699fb commit 355712f

File tree

4 files changed

+30
-56
lines changed

4 files changed

+30
-56
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"repositoryURL": "https://github.com/dmytro-anokhin/url-image.git",
77
"state": {
88
"branch": null,
9-
"revision": "22eca5ee4f317e0283da75426849ee23f0516022",
10-
"version": "0.9.19"
9+
"revision": "cedfdb207dcba2aec1229f3e2053bb93460a4244",
10+
"version": "2.1.9"
1111
}
1212
}
1313
]

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let package = Package(
2020
dependencies: [
2121
// Dependencies declare other packages that this package depends on.
2222
// .package(url: /* package url */, from: "1.0.0"),
23-
.package(url: "https://github.com/dmytro-anokhin/url-image.git", from: "0.9.15")
23+
.package(url: "https://github.com/dmytro-anokhin/url-image.git", from: "2.0.0")
2424
],
2525
targets: [
2626
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -65,58 +65,59 @@ struct ContentView: View {
6565

6666
# Customization
6767

68-
### HTTP Headers
68+
### Explicit Aspect Ratio
6969

70-
#### Availability: 1.0.15 or higher
70+
#### Availability: 1.0.21 or higher
7171

72-
The remote image viewer allows HTTP headers to be included in the URL request. To use them, pass a dictonary to the httpHeaders field. The format should be [Header: Value], both strings.
72+
An explcit image aspect ratio can be specified, which fixes an issue of incorrect stretching that occurs in certain situations. The `aspectRatio` parameter accepts `Binding<CGFloat>`
7373

7474
Example:
7575
```Swift
76-
import ImageViewerRemote
76+
import ImageViewer
7777

7878
struct ContentView: View {
7979
@State var showImageViewer: Bool = true
80+
@State var image = Image("example-image")
8081

8182
var body: some View {
8283
VStack {
8384
Text("Example!")
8485
}
8586
.frame(maxWidth: .infinity, maxHeight: .infinity)
86-
.overlay(ImageViewerRemote(imageURL: URL(string: "https://..."), viewerShown: self.$showImageViewer, httpHeaders: ["X-Powered-By": "Swift!"]))
87+
.overlay(ImageViewer(image: self.$image, viewerShown: self.$showImageViewer, aspectRatio: .constant(2)))
8788
}
8889
}
8990
```
9091

91-
### Explicit Aspect Ratio
92+
### Disable Cache
9293

93-
#### Availability: 1.0.21 or higher
94+
#### Availability: 1.0.25 or higher
9495

95-
An explcit image aspect ratio can be specified, which fixes an issue of incorrect stretching that occurs in certain situations. The `aspectRatio` parameter accepts `Binding<CGFloat>`
96+
To disable cache on the remote image viewer, simply pass a `Bool` value to the `disableCache` parameter
9697

9798
Example:
9899
```Swift
99-
import ImageViewer
100+
import ImageViewerRemote
100101

101102
struct ContentView: View {
102103
@State var showImageViewer: Bool = true
103-
@State var image = Image("example-image")
104104

105105
var body: some View {
106106
VStack {
107107
Text("Example!")
108108
}
109109
.frame(maxWidth: .infinity, maxHeight: .infinity)
110-
.overlay(ImageViewer(image: self.$image, viewerShown: self.$showImageViewer, aspectRatio: .constant(2)))
110+
.overlay(ImageViewerRemote(imageURL: URL(string: "https://..."), viewerShown: self.$showImageViewer, disableCache: true))
111111
}
112112
}
113113
```
114114

115-
### Disable Cache
115+
### HTTP Headers
116116

117-
#### Availability: 1.0.25 or higher
117+
#### Availability: 1.0.15 to 1.0.25
118+
#### *DEPRECATED*: No longer available as of 2.0.0
118119

119-
To disable cache on the remote image viewer, simply pass a `Bool` value to the `disableCache` parameter
120+
The remote image viewer allows HTTP headers to be included in the URL request. To use them, pass a dictonary to the httpHeaders field. The format should be [Header: Value], both strings.
120121

121122
Example:
122123
```Swift
@@ -130,7 +131,7 @@ struct ContentView: View {
130131
Text("Example!")
131132
}
132133
.frame(maxWidth: .infinity, maxHeight: .infinity)
133-
.overlay(ImageViewerRemote(imageURL: URL(string: "https://..."), viewerShown: self.$showImageViewer, disableCache: true))
134+
.overlay(ImageViewerRemote(imageURL: URL(string: "https://..."), viewerShown: self.$showImageViewer, httpHeaders: ["X-Powered-By": "Swift!"]))
134135
}
135136
}
136137
```

Sources/ImageViewerRemote/ImageViewerRemote.swift

+11-38
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,21 @@ public struct ImageViewerRemote: View {
1717

1818
@ObservedObject var loader: ImageLoader
1919

20-
public init(imageURL: Binding<String>, viewerShown: Binding<Bool>, httpHeaders: [String: String]? = nil, aspectRatio: Binding<CGFloat>? = nil, disableCache: Bool? = nil) {
20+
public init(imageURL: Binding<String>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, disableCache: Bool? = nil) {
2121
_imageURL = imageURL
2222
_viewerShown = viewerShown
23-
_httpHeaders = State(initialValue: httpHeaders)
2423
_disableCache = State(initialValue: disableCache)
2524
self.aspectRatio = aspectRatio
2625

27-
loader = ImageLoader(url: imageURL, httpHeaders: httpHeaders)
26+
loader = ImageLoader(url: imageURL)
2827
}
2928

30-
func getURLRequest(url: String, headers: [String: String]?) -> URLRequest {
29+
func getURLRequest(url: String) -> URLRequest {
3130
let url = URL(string: url)!
3231
var request = URLRequest(url: url)
3332
request.httpMethod = "GET"
3433

35-
if(headers != nil) {
36-
for (key, value) in headers! {
37-
print("adding header: \(key): \(value)")
38-
request.addValue("\(value)", forHTTPHeaderField: "\(key)")
39-
}
40-
41-
print("headers found, requesting URL image with headers: \(request.allHTTPHeaderFields)")
42-
return request;
43-
}
44-
else {
45-
print("headers nil, requesting URL image with request: \(request)")
46-
return request;
47-
}
34+
return request;
4835
}
4936

5037
@ViewBuilder
@@ -70,8 +57,8 @@ public struct ImageViewerRemote: View {
7057

7158
VStack {
7259
if(self.disableCache == nil || self.disableCache == false) {
73-
URLImage(getURLRequest(url: self.imageURL, headers: self.httpHeaders)) { proxy in
74-
proxy.image
60+
URLImage(url: URL(string: self.imageURL)!, content: { image in
61+
image
7562
.resizable()
7663
.aspectRatio(self.aspectRatio?.wrappedValue, contentMode: .fit)
7764
.offset(x: self.dragOffset.width, y: self.dragOffset.height)
@@ -90,7 +77,7 @@ public struct ImageViewerRemote: View {
9077
self.dragOffset = .zero
9178
}
9279
)
93-
}
80+
})
9481
}
9582
else {
9683
if loader.image != nil {
@@ -294,32 +281,18 @@ extension View {
294281
class ImageLoader: ObservableObject {
295282
@Published var image: UIImage?
296283
private let url: Binding<String>
297-
private let headers: [String: String]?
298284
private var cancellable: AnyCancellable?
299285

300-
func getURLRequest(url: String, headers: [String: String]?) -> URLRequest {
286+
func getURLRequest(url: String) -> URLRequest {
301287
let url = URL(string: url)!
302288
var request = URLRequest(url: url)
303289
request.httpMethod = "GET"
304290

305-
if(headers != nil) {
306-
for (key, value) in headers! {
307-
print("adding header: \(key): \(value)")
308-
request.addValue("\(value)", forHTTPHeaderField: "\(key)")
309-
}
310-
311-
print("headers found, requesting URL image with headers: \(request.allHTTPHeaderFields)")
312-
return request;
313-
}
314-
else {
315-
print("headers nil, requesting URL image with request: \(request)")
316-
return request;
317-
}
291+
return request;
318292
}
319293

320-
init(url: Binding<String>, httpHeaders: [String: String]?) {
294+
init(url: Binding<String>) {
321295
self.url = url
322-
self.headers = httpHeaders
323296

324297
load()
325298
}
@@ -329,7 +302,7 @@ class ImageLoader: ObservableObject {
329302
}
330303

331304
func load() {
332-
cancellable = URLSession.shared.dataTaskPublisher(for: getURLRequest(url: self.url.wrappedValue, headers: self.headers))
305+
cancellable = URLSession.shared.dataTaskPublisher(for: getURLRequest(url: self.url.wrappedValue))
333306
.map { UIImage(data: $0.data) }
334307
.replaceError(with: nil)
335308
.receive(on: DispatchQueue.main)

0 commit comments

Comments
 (0)