Simplify images, tag overview
This commit is contained in:
78
CHDataManagement/Generator/ImageVersion.swift
Normal file
78
CHDataManagement/Generator/ImageVersion.swift
Normal file
@@ -0,0 +1,78 @@
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
A version of an image with a specific size and possible different image type.
|
||||
*/
|
||||
struct ImageVersion {
|
||||
|
||||
/// The name of the image file to convert
|
||||
let image: FileResource
|
||||
|
||||
let type: FileType
|
||||
|
||||
let maximumWidth: Int
|
||||
|
||||
let maximumHeight: Int
|
||||
|
||||
let quality: CGFloat
|
||||
|
||||
init(image: FileResource, type: FileType, maximumWidth: CGFloat, maximumHeight: CGFloat, quality: CGFloat = 0.7) {
|
||||
self.image = image
|
||||
self.type = type
|
||||
self.maximumWidth = Int(maximumWidth)
|
||||
self.maximumHeight = Int(maximumHeight)
|
||||
self.quality = quality
|
||||
}
|
||||
|
||||
init(image: FileResource, type: FileType, maximumWidth: Int, maximumHeight: Int, quality: CGFloat = 0.7) {
|
||||
self.image = image
|
||||
self.type = type
|
||||
self.maximumWidth = maximumWidth
|
||||
self.maximumHeight = maximumHeight
|
||||
self.quality = quality
|
||||
}
|
||||
|
||||
/// A unique id of the version for this image (not unique across images)
|
||||
var versionId: String {
|
||||
"\(maximumWidth)-\(maximumHeight)-\(type.fileExtension!)"
|
||||
}
|
||||
|
||||
/// The path of the generated image version in the output folder (without leading slash)
|
||||
var outputPath: String {
|
||||
image.outputPath(width: maximumWidth, height: maximumHeight, type: type)
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageVersion: Identifiable {
|
||||
|
||||
var id: String {
|
||||
image.id + "-" + versionId
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageVersion: Equatable {
|
||||
|
||||
static func == (lhs: ImageVersion, rhs: ImageVersion) -> Bool {
|
||||
lhs.image.id == rhs.image.id &&
|
||||
lhs.maximumWidth == rhs.maximumWidth &&
|
||||
lhs.maximumHeight == rhs.maximumHeight &&
|
||||
lhs.type == rhs.type
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageVersion: Hashable {
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(image.id)
|
||||
hasher.combine(maximumWidth)
|
||||
hasher.combine(maximumHeight)
|
||||
hasher.combine(type)
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageVersion: Comparable {
|
||||
|
||||
static func < (lhs: ImageVersion, rhs: ImageVersion) -> Bool {
|
||||
lhs.id < rhs.id
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user