Skip to content

Instantly share code, notes, and snippets.

View nicolas-miari's full-sized avatar
💪
Livin' La Vida Low Carb

Nicolás Miari nicolas-miari

💪
Livin' La Vida Low Carb
View GitHub Profile
import Foundation
import CoreImage
/**
Based on: https://gist.github.com/SheffieldKevin/566dc048dd6f36716bcd
Updated for Swift 5.5 (Xcode 13)
*/
class ImageDiff {
func compare(leftImage: CGImage, rightImage: CGImage) throws -> Int {
@nicolas-miari
nicolas-miari / UIBezierPath+Superellipsoid
Created August 29, 2019 01:10
UIBezierPath extension to draw a 'squircle' (superellipsoid) of arbitrary size and "corner radius"
import UIKit
extension UIBezierPath {
/**
Draws the super-elliptic analog of a rounded rect. Unlike the regular rounded rect, the rounded corners
are the quadrants of a superellipse (i.e., parabolic segments), _not_ circular arcs.
If `rect` is a square and `cornerRadius` is equal to half the length or more, and actual superellipse is
drawn (no straight sections along its boundary) just like doing the same with a rounded rect results in a
circle being drawn.
@nicolas-miari
nicolas-miari / SolidImage.m
Created October 18, 2022 05:29
UIImage Create Solid
- (nullable UIImage *)sampleImage {
CGRect bounds = CGRectMake(0, 0, 50, 50);
UIColor *color = [UIColor colorWithDisplayP3Red:1 green:0.5 blue:0.25 alpha:1];
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
[color setFill];
UIRectFill(bounds);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
/// Make the ViewController conform to this protocol
///
protocol SceneKitViewController {
var sceneKitView: SceneKitView! { get }
}
/// Make the ViewController conform to this protocol too
///
protocol SpriteSceneNavigationDelegate {
func present(_ scene: BaseSpriteScene)
@nicolas-miari
nicolas-miari / Array+ExportGif.swift
Last active August 29, 2019 16:06
Extension to Array<UIImage> to export the images as an animated gif.
import UIKit
import MobileCoreServices // needed for kUTTypeGIF
extension Array where Element == UIImage {
/**
Writes a looping animated gif to the documents folder, using the receiver's elements as the frames.
Based on [this answer](https://stackoverflow.com/a/46095888/433373) from Stack Overflow.
@nicolas-miari
nicolas-miari / CGFloat+Tau.swift
Last active April 20, 2018 06:27
Tau Support for CGFloat
import CoreGraphics
extension CGFloat {
/// See this: https://tauday.com/tau-manifesto
static var tau: CGFloat {
return 2 * CGFloat.pi
}
}
@nicolas-miari
nicolas-miari / UIImage+CalculateOnScreenSize.swift
Last active May 22, 2017 07:54
UIImage Calculate On-Screen Size (UIImageView: AspectFit)
extension UIImage {
/**
Calculates the resulting size (rendered on screen) of the receiver when it
is displayed within a `UIImageView` instance of bounds size `containerSize`
that has its `contentMode` property set to `.ScaleAspectFit`.
- parameter contanerSize: The size of the hypothetical UIImageView instance
that is to display the receiver on screen.
- returns: the effective size at which the receiver would be rendered on
screen if displayed in said view.
@nicolas-miari
nicolas-miari / NSError+Shorthand.swift
Last active May 22, 2017 07:53
Swift extension that provides convenient shorthands for initializing `NSError` objects.
/**
Provides convenient shorthands for initializing `NSError` objects.
*/
extension NSError {
/**
Same as `init(domain:code:userInfo:)`, but `domain` defaults to the app's
bundle indentifier.
*/
convenience init(code: Int, userInfo: [NSObject : AnyObject]?) {
let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier!
@nicolas-miari
nicolas-miari / UIColor+CreateFromByteValues.swift
Last active May 22, 2017 07:52
Create a UIColor instance with `RGBA` values in the integer range [0, 255] (instead of float [0.0, 1.0])
import UIKit
extension UIColor {
convenience init(
redByte red: UInt8,
greenByte green: UInt8,
blueByte blue: UInt8,
alphaByte alpha: UInt8
) {
self.init(
@nicolas-miari
nicolas-miari / Image Scale Factor from File Name
Created December 18, 2014 02:07
Extracts the scale factor (e.g. 1x, 2x, 3x) of an image file name as a float.
- (CGFloat) imageScaleFactorFromFilename:(NSString*) imageFileName
{
NSString* filename = [imageFileName stringByDeletingPathExtension];
NSArray* components = [filename componentsSeparatedByString:@"@"];
if ([components count] > 1) {
// Hi res image
NSString* scaleSubstring = [components objectAtIndex:1]; // e.g. @"2x"