Skip to content

Commit

Permalink
Merge pull request #15 from dreipol/feature/image_tint
Browse files Browse the repository at this point in the history
Image manipulation & constraint improvements
  • Loading branch information
melbic authored Mar 10, 2021
2 parents 0ebca4e + c83eb88 commit cf0ec8e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 23 deletions.
64 changes: 64 additions & 0 deletions Sources/dreiKit/UIImage+Manipulation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// UIImage+Manipulation.swift
// dreiKit
//
// Created by Samuel Bichsel on 05.03.21.
//

import UIKit.UIImage

public extension UIImage {
func withTintByMultiply(with color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale); defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else {
return self
}

// flip the image
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0.0, y: -size.height)

// multiply blend mode
context.setBlendMode(.multiply)

let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage)
color.setFill()
context.fill(rect)

// create UIImage
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else {
return self
}

return newImage
}

// https://stackoverflow.com/questions/20021478/add-transparent-space-around-a-uiimage
func withPadding(_ padding: CGFloat) -> UIImage? {
return withPadding(x: padding, y: padding)
}

func withPadding(x: CGFloat, y: CGFloat) -> UIImage? {
let newWidth = size.width + 2 * x
let newHeight = size.height + 2 * y
let newSize = CGSize(width: newWidth, height: newHeight)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0); defer { UIGraphicsEndImageContext() }
let origin = CGPoint(x: (newWidth - size.width) / 2, y: (newHeight - size.height) / 2)
draw(at: origin)
return UIGraphicsGetImageFromCurrentImageContext()
}

func doubleHeight() -> UIImage {
let newHeight = size.height * 2
let newSize = CGSize(width: size.width, height: newHeight)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0); defer { UIGraphicsEndImageContext() }
let origin = CGPoint(x: 0, y: 0)
draw(at: origin)
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else {
return self
}

return newImage
}
}
69 changes: 47 additions & 22 deletions Sources/dreiKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@

import UIKit

public extension UIView {
public struct FillConstraints {
public var leading: NSLayoutConstraint
public var trailing: NSLayoutConstraint
public var top: NSLayoutConstraint
public var bottom: NSLayoutConstraint

public var all: [NSLayoutConstraint] { [leading, trailing, top, bottom] }
}

public extension UIView {
class func autoLayout() -> Self {
let view = self.init(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
return view
return self.init(frame: .zero).autolayout()
}

func autolayout() -> Self {
translatesAutoresizingMaskIntoConstraints = false
return self
}

func activateConstraints(
Expand All @@ -34,7 +45,7 @@ public extension UIView {
widthAnchor.constraint(equalTo: view.widthAnchor),
topAnchor.constraint(equalTo: view.topAnchor),
heightAnchor.constraint(equalTo: view.heightAnchor),
])
])
}

func fitVerticalScrollView() {
Expand All @@ -46,31 +57,40 @@ public extension UIView {
widthAnchor.constraint(equalTo: view.widthAnchor),
topAnchor.constraint(equalTo: view.topAnchor),
bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
])
}

func fillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) {
func createFillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) -> FillConstraints {
guard let view = superview else {
return
fatalError("superview is not set")
}
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: edgeInsets.leading),
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -edgeInsets.trailing),
topAnchor.constraint(equalTo: view.topAnchor, constant: edgeInsets.top),
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -edgeInsets.bottom),
])
return FillConstraints(
leading: leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: edgeInsets.leading),
trailing: trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -edgeInsets.trailing),
top: topAnchor.constraint(equalTo: view.topAnchor, constant: edgeInsets.top),
bottom: bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -edgeInsets.bottom)
)
}

func fillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) {
func fillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) {
NSLayoutConstraint.activate(createFillSuperview(edgeInsets: edgeInsets))
}

func createFillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) -> FillConstraints {
guard let view = superview else {
return
fatalError("superview is not set")
}
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: edgeInsets.leading),
trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -edgeInsets.trailing),
topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: edgeInsets.top),
bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -edgeInsets.bottom),
])

return FillConstraints(
leading: leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: edgeInsets.leading),
trailing: trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -edgeInsets.trailing),
top: topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: edgeInsets.top),
bottom: bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -edgeInsets.bottom)
)
}

func fillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) {
NSLayoutConstraint.activate(createFillSuperviewMargins(edgeInsets: edgeInsets))
}

func fillWithLowTrailingPriority() {
Expand Down Expand Up @@ -106,7 +126,12 @@ public extension UIView {
var layoutHeight: CGFloat {
return systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
}
}

public extension NSLayoutConstraint {
static func activate(_ constraints: FillConstraints) {
activate(constraints.all)
}
}

public protocol ViewInit: class {
Expand Down
2 changes: 1 addition & 1 deletion dreiKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "dreiKit"
s.version = "1.0.4"
s.version = "1.0.5"
s.summary = "A short description of dreiKit."
s.homepage = "https://github.com/dreipol/dreiKit"
s.license = { type: 'MIT', file: 'LICENSE' }
Expand Down

0 comments on commit cf0ec8e

Please sign in to comment.