Skip to content

Instantly share code, notes, and snippets.

View dhoerl's full-sized avatar

David Hoerl dhoerl

View GitHub Profile
@dhoerl
dhoerl / gist:c4b62696496f4a644239a950a1320cef
Last active June 27, 2023 04:34
Process Image Data in any orientation and make a UIImage in an "up" orientation
// Based on https://gist.github.com/schickling/b5d86cb070130f80bb40#gistcomment-2894406
func image(data: Data, orientation: UIImage.Orientation = .up) -> UIImage? {
let context: CGContext
let width: CGFloat
let height: CGFloat
func defaultImage() -> UIImage? {
return UIImage(data: data)
}
@dhoerl
dhoerl / gist:3acfadfa3de1fc97f2bb5c737ebb3d75
Last active March 31, 2020 19:49
Combine Complex Publisher for Medium
struct UpperCasePublisher: Publisher {
typealias Output = [Character]
typealias Failure = Error
let upstreamPublisher: AnyPublisher<Output, Error>
init(upstream: AnyPublisher<Output, Error>) {
self.upstreamPublisher = upstream
}
@dhoerl
dhoerl / gist:ec6c3688ed42f95989e52d007a66ed5c
Last active March 31, 2020 19:48
Combine Subscriber for Medium
final class StringSubscriber: Subscriber {
typealias Input = [Character]
typealias Failure = Error
var subscription: Subscription?
var count = 0
func receive(subscription: Subscription) {
self.subscription = subscription
self.subscription?.request(.max(1))
@dhoerl
dhoerl / gist:57fe946f95b647184c38b5c3942fc32c
Last active September 7, 2021 20:04
Combine Sample Publisher for Medium Article
enum SPErrors: Error {
case inputStringWasEmpty
}
struct StringPublisher: Publisher {
typealias Output = [Character]
typealias Failure = Error
private let data: [Character]
@dhoerl
dhoerl / gist:ea583ed8e9e94a6a44e276dc2ba55390
Last active February 14, 2020 16:24
Screenshot a UIView subsection in an UIImage
// Inspired by https://gist.github.com/nitrag/b3117a4b6b8e89fdbc12b98029cf98f8
+ (UIImage *)imageFromView:(UIView *)view subsection:(CGRect)subRect
{
// Image will be sized to the smaller rectangle
UIGraphicsBeginImageContextWithOptions(subRect.size, YES, 0);
// The primary view needs to shift up and left so the desired rect is visible
// But the rect passed below needs to be sized to the view, otherwise the image is compressed
CGRect drawRect = CGRectMake(-subRect.origin.x, -subRect.origin.x, view.bounds.size.width, view.bounds.size.height);
@dhoerl
dhoerl / Asymmetric.swift
Last active September 7, 2021 19:56
Implement asymmetric cryptography for distributed app where keys can both be stored as strings
//
// Asymmetric.swift
//
// Created by David Hoerl on 12/23/18.
// Copyright © 2018 David Hoerl. All rights reserved.
//
/*
This is the portion you need in the distributed app, which uses the public key that can appear in plain text
*/
@dhoerl
dhoerl / DavidTop10
Created May 13, 2015 14:15
Swift: David's Top 10 "Do This" List
David's Top 10: Do That When Writing Swift
1) Purchase Dash (OS X and iOS) and get Swift from SwiftDoc.org (invaluable reference, I use it daily!)
2) Use Enums for UIView tags and SegmentedControl segments;
enum MyViews: Int { case OKButton=1...} then switch MyViews(rawValue: view.tag)
also for states (somewhat replaces C's X-Macros, helps during debugging)
@dhoerl
dhoerl / AppDelegate(category).m
Created July 26, 2012 23:19
Keychain Wrapper for dictionary (NSData) not a single NSString
static NSString *kcIdentifier = @"MyApp";
@implementation LTAppDelegate (KeyChain)
- (void)keychainInit
{
self.keychainDict = [NSMutableDictionary dictionaryWithCapacity:7];
KeychainItemWrapper *item = [[KeychainItemWrapper alloc] initWithIdentifier:kcIdentifier accessGroup:nil];
[self.keychainDict setObject:@"" forKey:kcEmailAddress];
@dhoerl
dhoerl / freeDiskSpace.m
Created June 20, 2012 01:15
Getting the Free Disk Space on an iOS device
- (uint64_t)freeDiskspace
{
uint64_t totalFreeSpace = 0;
__autoreleasing NSError *error = nil;
NSString *path = [self applicationDocumentsDirectory];
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:path error: &error];
if (dictionary) {
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
@dhoerl
dhoerl / README.txt
Created January 29, 2012 22:53
std::string using printf style format strings
Prefer printf to the C++ iosstream methods? Now you can create a string using printf style format strings, and also append formatted string to existing strings.
Inspired by: http://stackoverflow.com/questions/2342162