Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Allow setting image scale factor in initializer #169

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Sources/URLImage/Common/TransientImage+SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import Model

@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
public extension TransientImage {

var image: Image {
func image(scale: CGFloat) -> Image {
let orientation = Image.Orientation(cgOrientation)
return Image(decorative: self.cgImage, scale: 1.0, orientation: orientation)
return Image(decorative: self.cgImage, scale: scale, orientation: orientation)
}
}
4 changes: 2 additions & 2 deletions Sources/URLImage/Service/URLImageService+Decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension URLImageService {
switch result {
case .data(let data):

guard let transientImage = TransientImage(data: data, maxPixelSize: options.maxPixelSize) else {
guard let transientImage = TransientImage(data: data, maxPixelSize: nil) else {
throw URLImageError.decode
}

Expand All @@ -33,7 +33,7 @@ extension URLImageService {

let location = URL(fileURLWithPath: path)

guard let transientImage = TransientImage(location: location, maxPixelSize: options.maxPixelSize) else {
guard let transientImage = TransientImage(location: location, maxPixelSize: nil) else {
throw URLImageError.decode
}

Expand Down
25 changes: 22 additions & 3 deletions Sources/URLImage/URLImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public extension URLImage {

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder empty: @escaping () -> Empty,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
Expand All @@ -81,12 +82,13 @@ public extension URLImage {
inProgress: inProgress,
failure: failure,
content: { (transientImage: TransientImage) -> Content in
content(transientImage.image)
content(transientImage.image(scale: scale))
})
}

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder empty: @escaping () -> Empty,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
Expand All @@ -98,7 +100,7 @@ public extension URLImage {
inProgress: inProgress,
failure: failure,
content: { (transientImage: TransientImage) -> Content in
content(transientImage.image, transientImage.info)
content(transientImage.image(scale: scale), transientImage.info)
})
}
}
Expand All @@ -109,12 +111,14 @@ public extension URLImage where Empty == EmptyView {

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
@ViewBuilder content: @escaping (_ image: Image) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: inProgress,
failure: failure,
Expand All @@ -123,12 +127,14 @@ public extension URLImage where Empty == EmptyView {

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
@ViewBuilder content: @escaping (_ image: Image, _ info: ImageInfo) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: inProgress,
failure: failure,
Expand All @@ -143,11 +149,13 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
@ViewBuilder content: @escaping (_ image: Image) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: { _ in ActivityIndicator() },
failure: failure,
Expand All @@ -156,11 +164,13 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
@ViewBuilder content: @escaping (_ image: Image, _ info: ImageInfo) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: { _ in ActivityIndicator() },
failure: failure,
Expand All @@ -175,11 +185,13 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder content: @escaping (_ image: Image) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: inProgress,
failure: { _, _ in EmptyView() },
Expand All @@ -188,11 +200,13 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder inProgress: @escaping (_ progress: Float?) -> InProgress,
@ViewBuilder content: @escaping (_ image: Image, _ info: ImageInfo) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: inProgress,
failure: { _, _ in EmptyView() },
Expand All @@ -208,10 +222,12 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder content: @escaping (_ image: Image) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: { _ in ActivityIndicator() },
failure: { _, _ in EmptyView() },
Expand All @@ -220,10 +236,12 @@ public extension URLImage where Empty == EmptyView,

init(_ url: URL,
identifier: String? = nil,
scale: CGFloat = 1,
@ViewBuilder content: @escaping (_ image: Image, _ info: ImageInfo) -> Content) {

self.init(url,
identifier: identifier,
scale: scale,
empty: { EmptyView() },
inProgress: { _ in ActivityIndicator() },
failure: { _, _ in EmptyView() },
Expand Down Expand Up @@ -276,9 +294,10 @@ public extension URLImage where InProgress == Content,
Empty == Content,
Failure == Content {

init(url: URL, @ViewBuilder content: @escaping (_ phase: URLImagePhase) -> Content) {
init(url: URL, scale: CGFloat = 1, @ViewBuilder content: @escaping (_ phase: URLImagePhase) -> Content) {
self.init(url,
identifier: nil,
scale: scale,
empty: { content(.empty) },
inProgress: { _ in content(.empty) },
failure: { error, retry in content(.failure(error)) },
Expand Down