Skip to content

Commit

Permalink
Allow to set only height as downsampling size. (#11)
Browse files Browse the repository at this point in the history
* Use swift-docc-plugin v1.3.0

* Update README

* Create DownSamplingSize to allow either width or heght as size defining parameter.

* Update downsampling code to follow the parameter change.

* Delete unused init

* Update Example

* Update Example

* Refactor
  • Loading branch information
fummicc1 authored Jul 6, 2024
1 parent 9b7e3a8 commit 27dab11
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1540"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AsyncDownSamplingImage"
BuildableName = "AsyncDownSamplingImage"
BlueprintName = "AsyncDownSamplingImage"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AsyncDownSamplingImageTests"
BuildableName = "AsyncDownSamplingImageTests"
BlueprintName = "AsyncDownSamplingImageTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AsyncDownSamplingImage"
BuildableName = "AsyncDownSamplingImage"
BlueprintName = "AsyncDownSamplingImage"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
28 changes: 20 additions & 8 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@

/* Begin PBXBuildFile section */
D30592B4298C9F830060D5EE /* AsyncDownSamplingImage in Frameworks */ = {isa = PBXBuildFile; productRef = D30592B3298C9F830060D5EE /* AsyncDownSamplingImage */; };
D36AB81B2C38C38500F37D8F /* DownsampleGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36AB81A2C38C38500F37D8F /* DownsampleGridView.swift */; };
D36AB81D2C38C3F900F37D8F /* DownSamplingVStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36AB81C2C38C3F900F37D8F /* DownSamplingVStackView.swift */; };
D36AB81F2C38C48400F37D8F /* StandardView+Grid.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36AB81E2C38C48400F37D8F /* StandardView+Grid.swift */; };
D36AB8212C38C4AB00F37D8F /* StandardView+VStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36AB8202C38C4AB00F37D8F /* StandardView+VStack.swift */; };
D36AB8232C38C74300F37D8F /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36AB8222C38C74300F37D8F /* Util.swift */; };
D3E99FFE298C9EEF006E08E2 /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E99FFD298C9EEF006E08E2 /* ExampleApp.swift */; };
D3E9A000298C9EEF006E08E2 /* DefaultContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E99FFF298C9EEF006E08E2 /* DefaultContentView.swift */; };
D3E9A002298C9EF1006E08E2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3E9A001298C9EF1006E08E2 /* Assets.xcassets */; };
D3E9A006298C9EF1006E08E2 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3E9A005298C9EF1006E08E2 /* Preview Assets.xcassets */; };
D3F22F9B29900A2E005365E3 /* DownsampleContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3F22F9A29900A2E005365E3 /* DownsampleContentView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D30592B1298C9F7F0060D5EE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
D36AB81A2C38C38500F37D8F /* DownsampleGridView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DownsampleGridView.swift; sourceTree = "<group>"; };
D36AB81C2C38C3F900F37D8F /* DownSamplingVStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownSamplingVStackView.swift; sourceTree = "<group>"; };
D36AB81E2C38C48400F37D8F /* StandardView+Grid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StandardView+Grid.swift"; sourceTree = "<group>"; };
D36AB8202C38C4AB00F37D8F /* StandardView+VStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StandardView+VStack.swift"; sourceTree = "<group>"; };
D36AB8222C38C74300F37D8F /* Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = "<group>"; };
D3E99FFA298C9EEF006E08E2 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
D3E99FFD298C9EEF006E08E2 /* ExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleApp.swift; sourceTree = "<group>"; };
D3E99FFF298C9EEF006E08E2 /* DefaultContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultContentView.swift; sourceTree = "<group>"; };
D3E9A001298C9EF1006E08E2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D3E9A003298C9EF1006E08E2 /* Example.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example.entitlements; sourceTree = "<group>"; };
D3E9A005298C9EF1006E08E2 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
D3E9A00D298C9F06006E08E2 /* AsyncDownSamplingImage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = AsyncDownSamplingImage; path = ..; sourceTree = "<group>"; };
D3F22F9A29900A2E005365E3 /* DownsampleContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownsampleContentView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -69,9 +75,12 @@
children = (
D3E9A003298C9EF1006E08E2 /* Example.entitlements */,
D30592B1298C9F7F0060D5EE /* Info.plist */,
D3E99FFF298C9EEF006E08E2 /* DefaultContentView.swift */,
D3F22F9A29900A2E005365E3 /* DownsampleContentView.swift */,
D36AB81A2C38C38500F37D8F /* DownsampleGridView.swift */,
D36AB81C2C38C3F900F37D8F /* DownSamplingVStackView.swift */,
D3E99FFD298C9EEF006E08E2 /* ExampleApp.swift */,
D36AB81E2C38C48400F37D8F /* StandardView+Grid.swift */,
D36AB8202C38C4AB00F37D8F /* StandardView+VStack.swift */,
D36AB8222C38C74300F37D8F /* Util.swift */,
D3E9A001298C9EF1006E08E2 /* Assets.xcassets */,
D3E9A004298C9EF1006E08E2 /* Preview Content */,
);
Expand Down Expand Up @@ -167,9 +176,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D3E9A000298C9EEF006E08E2 /* DefaultContentView.swift in Sources */,
D36AB81B2C38C38500F37D8F /* DownsampleGridView.swift in Sources */,
D36AB8232C38C74300F37D8F /* Util.swift in Sources */,
D36AB81D2C38C3F900F37D8F /* DownSamplingVStackView.swift in Sources */,
D36AB81F2C38C48400F37D8F /* StandardView+Grid.swift in Sources */,
D36AB8212C38C4AB00F37D8F /* StandardView+VStack.swift in Sources */,
D3E99FFE298C9EEF006E08E2 /* ExampleApp.swift in Sources */,
D3F22F9B29900A2E005365E3 /* DownsampleContentView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "10bc670db657d11bdd561e07de30a9041311b2b1",
"version" : "1.1.0"
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
Expand Down
44 changes: 44 additions & 0 deletions Example/Example/DownSamplingVStackView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// DownSamplingVStackView.swift
// Example
//
// Created by Fumiya Tanaka on 2024/07/06.
//

import SwiftUI
import AsyncDownSamplingImage

struct DownSamplingVStackView: View {

@State private var url = Util.VStack.url
@State private var height: Double = 240

var body: some View {
VStack {
Text("AsyncDownSamplingImage")
ScrollView {
LazyVStack() {
ForEach(0..<1000, id: \.self) { _ in
AsyncDownSamplingImage(
url: url,
downsampleSize: .height(
Util.VStack.bufferedImageHeight
)
) { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: height)
} onFail: { error in
Text("Error: \(error.localizedDescription)")
}
}
}
}
}
.padding()
}
}

#Preview {
DownSamplingVStackView()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import SwiftUI
import AsyncDownSamplingImage

struct DownsampleContentView: View {
struct DownsampleGridView: View {

@State private var url = URL(string: "https://picsum.photos/1000")
@State private var url = Util.Grid.url
@State private var size: CGSize = .init(width: 160, height: 160)

var body: some View {
Expand All @@ -14,11 +14,15 @@ struct DownsampleContentView: View {
ForEach(0..<1000, id: \.self) { _ in
AsyncDownSamplingImage(
url: url,
downsampleSize: size
downsampleSize: .size(Util.Grid.bufferedImageSize)
) { image in
image.resizable()
.frame(width: size.width, height: size.height)
} fail: { error in
.aspectRatio(contentMode: .fit)
.frame(
width: size.width,
height: size.height
)
} onFail: { error in
Text("Error: \(error.localizedDescription)")
}
}
Expand All @@ -31,6 +35,6 @@ struct DownsampleContentView: View {

struct DownsampleContentView_Previews: PreviewProvider {
static var previews: some View {
DownsampleContentView()
DownsampleGridView()
}
}
22 changes: 20 additions & 2 deletions Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@ import SwiftUI
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
DefaultContentView()
DownsampleContentView()
TabView {
VStack {
StandardView_Grid()
DownsampleGridView()
}
.tabItem {
Image(systemName: "1.circle")
Text("Grid")
}
.tag("Grid")
VStack {
StandardView_VStack()
DownSamplingVStackView()
}
.tabItem {
Image(systemName: "2.circle")
Text("VStack")
}
.tag("VStack")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SwiftUI

struct DefaultContentView: View {
struct StandardView_Grid: View {

@State private var url = URL(string: "https://picsum.photos/1000")
@State private var url = Util.Grid.url
@State private var size: CGSize = .init(width: 160, height: 160)

var body: some View {
Expand All @@ -19,7 +19,11 @@ struct DefaultContentView: View {
Text("Error: \(error.localizedDescription)")
case .success(let image):
image.resizable()
.frame(width: size.width, height: size.height)
.aspectRatio(contentMode: .fit)
.frame(
width: size.width,
height: size.height
)
@unknown default:
fatalError()
}
Expand All @@ -32,8 +36,6 @@ struct DefaultContentView: View {
}
}

struct DefaultContentView_Previews: PreviewProvider {
static var previews: some View {
DefaultContentView()
}
#Preview {
StandardView_Grid()
}
45 changes: 45 additions & 0 deletions Example/Example/StandardView+VStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// StandardView+VStack.swift
// Example
//
// Created by Fumiya Tanaka on 2024/07/06.
//

import SwiftUI

struct StandardView_VStack: View {

@State private var url = Util.VStack.url
@State private var height: Double = 240

var body: some View {
VStack {
Text("Default AsyncImage")
ScrollView {
LazyVStack() {
ForEach(0..<1000, id: \.self) { _ in
AsyncImage(url: url) { phase in
switch phase {
case .empty:
ProgressView().progressViewStyle(.circular)
case .failure(let error):
Text("Error: \(error.localizedDescription)")
case .success(let image):
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: height)
@unknown default:
fatalError()
}
}
}
}
}
}
.padding()
}
}

#Preview {
StandardView_VStack()
}
19 changes: 19 additions & 0 deletions Example/Example/Util.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Util.swift
// Example
//
// Created by Fumiya Tanaka on 2024/07/06.
//

import Foundation

enum Util {
enum Grid {
static let url = URL(string: "https://picsum.photos/1000/1000")
static let bufferedImageSize = CGSize(width: 160, height: 160)
}
enum VStack {
static let url = URL(string: "https://picsum.photos/800/600")
static let bufferedImageHeight = 320.0
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(
url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"
url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"
)
],
targets: [
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ Pull requests, bug reports and feature requests are welcome 🚀
# License

[MIT LICENSE](https://github.com/fummicc1/AsyncDownSamplingImage/blob/main/LICENSE)

# Reference

- https://medium.com/@zippicoder/downsampling-images-for-better-memory-consumption-and-uicollectionview-performance-35e0b4526425
Loading

0 comments on commit 27dab11

Please sign in to comment.