Skip to content

Commit

Permalink
Align interface with AsyncImage
Browse files Browse the repository at this point in the history
  • Loading branch information
leoz committed Sep 10, 2023
1 parent 0d99529 commit cfb4109
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
11 changes: 8 additions & 3 deletions Demo/CachedImageDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ struct ContentView: View {
List(posters, id: \.self) { url in
CachedImage(
url: url,
placeholder: { Text("Loading ...") },
content: { image in
image.resizable()
image
.resizable()
.aspectRatio(contentMode: .fit)
},
placeholder: {
Text("Loading ...")
}
)
.frame(idealHeight: geometry.size.width / 2 * 3) // 2:3 aspect ratio
.frame(
idealHeight: geometry.size.width / 2 * 3
) // 2:3 aspect ratio
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions Sources/CachedImage/CachedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
import SwiftUI

public struct CachedImage<Placeholder: View, Content: View>: View {
@StateObject private var loader: ImageLoader
private let placeholder: Placeholder
private let content: (Image) -> Content
private let url: URL
private let content: (Image) -> Content
private let placeholder: Placeholder
@StateObject private var loader: ImageLoader

public init(
url: URL,
placeholder: @escaping () -> Placeholder,
content: @escaping (Image) -> Content
content: @escaping (Image) -> Content,
placeholder: @escaping () -> Placeholder
) {
self.placeholder = placeholder()
self.content = content
self.url = url
_loader = StateObject(wrappedValue: ImageLoader(url: url, cache: Environment(\.imageCache).wrappedValue))
self.content = content
self.placeholder = placeholder()
_loader = StateObject(
wrappedValue: ImageLoader(
url: url,
cache: Environment(\.imageCache).wrappedValue
)
)
}

public var body: some View {
Expand Down

0 comments on commit cfb4109

Please sign in to comment.