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
Changes from 2 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
8 changes: 8 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
@@ -9,13 +9,21 @@
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")

/// Optionally, you can set predefined directory for where to save files
public var directoryUrl: URL? = nil

public var uiKitImageFormat: UIKitImageFormat = .jpg

public var encoder: JSONEncoder = JSONEncoder()
public var decoder: JSONDecoder = JSONDecoder()

2 changes: 1 addition & 1 deletion Sources/Storage+Image.swift
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
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, option: options), StorageError.encodeData)
})
}

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

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