Skip to content

Instantly share code, notes, and snippets.

View StanDimitroff's full-sized avatar

Stanislav Dimitrov StanDimitroff

View GitHub Profile
import SwiftUI
struct CustomVStack<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack(spacing: 0) {
@StanDimitroff
StanDimitroff / run_lenght_decoding.py
Created May 7, 2018 12:22
Run-length decoding implementation in python
import re
def decode(text):
"""
Decodes the text using run-length encoding
"""
return re.sub(r'(\D)(\d*)', lambda m: m.group(1) * int(m.group(2)) if m.group(2) != '' else m.group(1), text)
@StanDimitroff
StanDimitroff / run_lenght_encoding.py
Last active May 7, 2018 07:17
Run-length encoding implementation in Python
from itertools import groupby
def encode(text):
"""
Returns the run-length encoded version of the text
(numbers after symbols, length = 1 is skipped)
"""
splitted = list(text)
grouped = [list(j) for i, j in groupby(splitted)]
typealias SortDescriptor<Value> = (Value, Value) -> Bool
protocol Sortable: class {
var sortRules: [SortRule] { get }
var currentRule: SortRule { get }
func sortData()
}
extension Sortable {
struct MultipartRequest {
private let boundary = UUID().uuidString
private var bodyString: String = ""
private var body = Data()
var request: URLRequest
init(
url: URL,
@StanDimitroff
StanDimitroff / Optional+Throws.swift
Last active March 14, 2018 13:18
Throwable Optionals
public enum ThrowableOptionalError: Error {
case unableToUnwrap
}
postfix operator +!
extension Optional {
public func unwrap() throws -> Wrapped {
switch self {
case .some(let value):
@StanDimitroff
StanDimitroff / CGPoint+Scale.swift
Created February 16, 2018 10:38
CGPoint scale
extension CGPoint {
func scaled(to size: CGSize) -> CGPoint {
return CGPoint(x: self.x * size.width, y: self.y * size.height)
}
}
@StanDimitroff
StanDimitroff / UIImage+Clipp.swift
Last active April 25, 2024 04:52
Image clipping to UIBezierPath
extension UIImage {
func imageByApplyingClippingBezierPath(_ path: UIBezierPath) -> UIImage {
// Mask image using path
guard let let maskedImage = imageByApplyingMaskingBezierPath(path) else { return nil }
// Crop image to frame of path
let croppedImage = UIImage(cgImage: maskedImage.cgImage!.cropping(to: path.bounds)!)
return croppedImage
}
@StanDimitroff
StanDimitroff / UIImage+Crop.swift
Created February 16, 2018 10:16
Image cropping to AVCaptureVideoPreviewLayer rect
extension UIImage {
func crop(toPreviewLayer layer: AVCaptureVideoPreviewLayer, withRect rect: CGRect) -> UIImage {
let outputRect = layer.metadataOutputRectConverted(fromLayerRect: rect)
var cgImage = self.cgImage!
let width = CGFloat(cgImage.width)
let height = CGFloat(cgImage.height)
let cropRect = CGRect(
x: outputRect.origin.x * width,
y: outputRect.origin.y * height,
width: outputRect.size.width * width,
@StanDimitroff
StanDimitroff / UIImage+CIPerspectiveCorrection.swift
Last active June 21, 2023 04:49
Image perspective correction
extension UIImage {
var flattened: UIImage? {
let ciImage = CIImage(image: self)!
guard let openGLContext = EAGLContext(api: .openGLES2) else { return nil }
let ciContext = CIContext(eaglContext: openGLContext)
let detector = CIDetector(ofType: CIDetectorTypeRectangle,
context: ciContext,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])!