Skip to content

Instantly share code, notes, and snippets.

View ksm's full-sized avatar
🏔️

Karol S. Mazur ksm

🏔️
View GitHub Profile
#!/bin/bash
#
# Written by Corey Haines
# Scriptified by Gary Bernhardt
#
# Put this anywhere on your $PATH (~/bin is recommended). Then git will see it
# and you'll be able to do `git churn`.
#
# Show churn for whole repo:
# $ git churn
@ksm
ksm / gist:1869823
Created February 20, 2012 15:57
CALayer draw resized image (kill all misaligned images)
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
UIImage *image = [self loadImage];
if (image != nil) {
CGSize s = image.size;
CGRect r = layer.bounds;
@ksm
ksm / Embedding.swift
Last active June 14, 2018 18:30
Lets a view controller embed a child view controller.
import UIKit
protocol Embedding: class {
var embeddedViewController: UIViewController? { get set }
}
enum EmbeddingAnimation {
case none
case flipFromLeft
case crossDissolve
@ksm
ksm / SecRequestSharedWebCredential.swift
Last active October 18, 2017 21:50
Handling SecRequestSharedWebCredential that works with Xcode 7.2 and Swift 2.
SecRequestSharedWebCredential(nil, nil) { credentials, error in
guard error == nil else {
let cocoaError = error! as NSError
let errorIsExpected = cocoaError.domain == NSOSStatusErrorDomain && cocoaError.code == Int(errSecItemNotFound)
if !errorIsExpected {
// Do something with an unexpected error.
}
return
@ksm
ksm / LoadThumbnail.swift
Created June 17, 2016 17:36
One of many Photos.framework idiosyncrasies
/*
If you run loadThumbnail() in an *iPhone-only app that is installed on the iPad*,
the Photos framework will callback first with a degraded version of the requested asset image,
and then it will callback a second time with a nil image.
If you're not careful and you pass the nil image to your UIImageView, then you will never see the thumbnail.
The UIImageView will first get set with a proper UIImage, then soon after with a nil UIImage.
The solution is to ignore or discard any image that is nil.
So what happens if you try to setup PHImageRequestOptions to make sure that Photos framework only calls back once?
@ksm
ksm / gist:1875946
Created February 21, 2012 11:20
CALayer shadowPath done the right way
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
// setup the layer
CALayer *layer = view.layer;
layer.bounds = sublayer_bounds;
layer.backgroundColor = random_color();
// set the shadow properties on the layer
@ksm
ksm / gist:3029763
Created July 1, 2012 21:57
Batching up database changes into a single save
// Source: Stanford CS193P November 16, 2010 Lecture 16
// Use case: lots of Core Data changes. You want them to save once they settle down.
- (void)delayedSave:(NSManagedObjectContext *)ctxt
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doSave:) object:ctxt];
[self performSelector:@selector(doSave:) withObject:ctxt afterDelay:1.0];
}
@ksm
ksm / gist:3951742
Created October 25, 2012 09:55
Enlarge tappable area of a UIView
// Source: WWDC 2012 Session 216
// Advanced Appearance Customization on iOS
// Rather useful when your button is smaller than the golden 44 points
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat widthDelta = 44.0 - bounds.size.width;
CGFloat heightDelta = 44.0 - bounds.size.height;
CGRect bounds = CGRectInset(self.bounds, -0.5 * widthDelta, -0.5 * heightDelta);
return CGRectContainsPoint(bounds, point);
@ksm
ksm / gist:5900007
Created July 1, 2013 11:10
Transliterate and normalize alphabets to latin
// Source: WWDC 2013 Session 228
CFMutableStringRef string = (__bridgeCFMutableStringRef)[@"Hello!こんにちは!สวสัดี!مرحبا!您好!" mutableCopy];
CFStringTransform(string, NULL, kCFStringTransformToLatin, NO);
// Hello! kon'nichiha! swạsdī! mrḥbạ! nín hǎo!
CFStringTransform(string, NULL, kCFStringTransformStripCombiningMarks, NO);
// Hello! kon'nichiha! swasdi! mrhba! nin hao!
@ksm
ksm / gist:4088176
Created November 16, 2012 15:25
Extract date from natural language string
/*
Extract date from natural language string
Source: WWDC2012 Session 215 - Text and Linguistic Analysis
*/
NSDataDetector *dateDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:nil];
NSString *dateString = @"13th September 1986 11:59pm";
NSTextCheckingResult *result = [dateDetector firstMatchInString:dateString
options:0
range:NSMakeRange(0, [dateString length])];