Skip to content

Instantly share code, notes, and snippets.

View PimCoumans's full-sized avatar
thinking about making coffee

Pim PimCoumans

thinking about making coffee
View GitHub Profile
@StefanJager
StefanJager / UIColor+Components.swift
Last active June 16, 2022 08:52
A more SWIFT-friendly approach to getting components from an UIColor
//
// UIColor+Components.swift
//
// Copyright (c) 2015 Stefan Jager
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
@Koze
Koze / PhotosScreenshot.m
Last active November 30, 2022 22:12
Getting All Screenshots with Photos.framework
NSMutableArray *mArray = [NSMutableArray array];
// fetch all image assets
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
PHFetchResult *result = [PHAsset fetchAssetsWithOptions:fetchOptions];
[result enumerateObjectsUsingBlock:^(PHAsset * __nonnull asset, NSUInteger idx, BOOL * __nonnull stop) {
// filter with subtype for screenshot
if (asset.mediaSubtypes & PHAssetMediaSubtypePhotoScreenshot) {
[mArray addObject:asset];
@endavid
endavid / FontAtlas.swift
Created May 27, 2017 18:03
Signed Distance Field in Metal/Swift
public class FontAtlas: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool { get { return true } }
static let atlasSize: Int = 2048 // 4096 runs out of mem...
var glyphs : [GlyphDescriptor] = []
let parentFont: UIFont
var fontPointSize: CGFloat
let textureSize: Int
var textureData: [UInt8] = []
@lattner
lattner / TaskConcurrencyManifesto.md
Last active April 6, 2025 10:44
Swift Concurrency Manifesto
@douglashill
douglashill / KeyboardTableView.swift
Last active March 30, 2023 22:01
A UITableView that allows navigation and selection using a hardware keyboard.
// Douglas Hill, December 2018
// Made for https://douglashill.co/reading-app/
// Find the latest version of this file at https://github.com/douglashill/KeyboardKit
import UIKit
/// A table view that allows navigation and selection using a hardware keyboard.
/// Only supports a single section.
class KeyboardTableView: UITableView {
// These properties may be set or overridden to provide discoverability titles for key commands.
@adamgraham
adamgraham / UIColor+CIELCh.swift
Last active December 3, 2023 10:20
An extension of the iOS class UIColor to provide conversion to and from CIELCh° colors.
/// An extension to provide conversion to and from CIELCh° colors.
extension UIColor {
/// The CIELCh° components of a color - lightness (L), chroma (C), and hue (h).
struct CIELCh: Hashable {
/// The lightness component of the color, in the range [0, 100] (darkest to brightest).
var L: CGFloat
/// The chroma component of the color.
var C: CGFloat
@AliSoftware
AliSoftware / Bindings.swift
Last active March 26, 2025 12:10
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
import UIKit
import PlaygroundSupport
import CoreGraphics
extension Numeric {
/// Linear interpolation
/// - Parameters:
/// - a: a value to interpolate from
/// - b: a value to interpolate to
/// - delta: interpolation delta value (0 - 1)
extension Result {
public func `catch`(_ handler: () throws -> Success) -> Result<Success, Error> {
flatMapError { _ in
.init { try handler() }
}
}
public func `catch`(_ handler: (Failure) throws -> Success) -> Result<Success, Error> {
flatMapError { error in
.init { try handler(error) }
@moyerr
moyerr / AnyAsyncSequence.swift
Created May 17, 2023 17:54
A type-erased AsyncSequence
struct AnyAsyncSequence<Element>: AsyncSequence {
struct AnyAsyncIterator: AsyncIteratorProtocol {
private let _next: () async throws -> Element?
init<Base: AsyncSequence>(_ base: Base) where Base.Element == Element {
var baseIterator = base.makeAsyncIterator()
self._next = { try await baseIterator.next() }
}
func next() async throws -> Element? {