Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to save as PNG #24

Open
wants to merge 6 commits into
base: master
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
6 changes: 6 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import Foundation

public struct Options {

public enum UIKitImageFormat {
case png
case jpg
}

/// By default, files are saved into searchPathDirectory/folder
public var searchPathDirectory: FileManager.SearchPathDirectory
public var folder: String = (Bundle.main.bundleIdentifier ?? "").appending("/Default")
Expand Down
14 changes: 13 additions & 1 deletion Sources/Storage+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
public extension Storage {
func save(object: Image, forKey key: String) throws {
try commonSave(object: object as AnyObject, forKey: key, toData: {
return try unwrapOrThrow(Utils.data(image: object), StorageError.encodeData)
return try unwrapOrThrow(
Utils.data(image: object, uiKitImageFormat: .jpg),
StorageError.encodeData
)
})
}

func save(object: Image, uiKitImageFormat: Options.UIKitImageFormat, forKey key: String) throws {
try commonSave(object: object as AnyObject, forKey: key, toData: {
return try unwrapOrThrow(
Utils.data(image: object, uiKitImageFormat: uiKitImageFormat),
StorageError.encodeData
)
})
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public class Storage {
public func fileUrl(forKey key: String) -> URL {
return folderUrl.appendingPathComponent(key, isDirectory: false)
}

public func fileList() throws -> [String] {
return try fileManager.contentsOfDirectory(atPath: folderUrl.path)
}
}

extension Storage {
Expand Down
9 changes: 7 additions & 2 deletions Sources/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ class Utils {
#endif
}

static func data(image: Image) -> Data? {
static func data(image: Image, uiKitImageFormat: Options.UIKitImageFormat) -> Data? {
#if canImport(UIKit)
return image.jpegData(compressionQuality: 0.9)
switch uiKitImageFormat {
case .jpg:
return image.jpegData(compressionQuality: 0.9)
case .png:
return image.pngData()
}
#elseif canImport(AppKit)
return image.tiffRepresentation
#else
Expand Down