Skip to content

Instantly share code, notes, and snippets.

View levantAJ's full-sized avatar
💭
🌵🌵🌵

levantAJ levantAJ

💭
🌵🌵🌵
View GitHub Profile
@levantAJ
levantAJ / ExifOrientation.swift
Last active April 12, 2017 09:48
Exif Orientation from current device orientation
var exifOrientation: Int {
let exifOrientation: DeviceOrientation
enum DeviceOrientation: Int {
case top0ColLeft = 1
case top0ColRight = 2
case bottom0ColRight = 3
case bottom0ColLeft = 4
case left0ColTop = 5
case right0ColTop = 6
case right0ColBottom = 7
@levantAJ
levantAJ / UIImageToCMSampleBuffer
Last active October 12, 2022 03:02
Create CMSampleBuffer from UIImage
import ImageIO
import AVFoundation
var cvPixelBuffer: CVPixelBuffer? {
var pixelBuffer: CVPixelBuffer? = nil
let options: [NSObject: Any] = [
kCVPixelBufferCGImageCompatibilityKey: false,
kCVPixelBufferCGBitmapContextCompatibilityKey: false,
]
let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(size.width), Int(size.height), kCVPixelFormatType_32BGRA, options as CFDictionary, &pixelBuffer)
@levantAJ
levantAJ / AssetRecorderView.swift
Last active April 8, 2021 06:31
Record video, with auto detect faces, and overlay mask into faces
//
// AssetRecorderView.swift
// Snowball
//
// Created by Le Tai on 7/20/16.
// Copyright © 2016 Snowball. All rights reserved.
//
import UIKit
import AVFoundation
extension Date {
var timeAgoSimple: String {
let deltaSeconds = fabs(timeIntervalSince(Date()))
let deltaMinutes = deltaSeconds / 60.0
if deltaSeconds < 60 {
return "\(Int(deltaSeconds))s"
} else if deltaMinutes < 60 {
return "\(Int(deltaMinutes))m"
} else if deltaMinutes < (24 * 60) {
@levantAJ
levantAJ / UIViewController.swift
Created May 11, 2017 10:28
Load list of segues in UIViewController
import UIKit
extension UIViewController {
var segueIdentifiers: [String] {
return (self.value(forKey: "storyboardSegueTemplates") as? [AnyObject])?.flatMap({ $0.value(forKey: "identifier") as? String }) ?? []
}
}
@levantAJ
levantAJ / ViewController.swift
Created May 26, 2017 07:30
Hide status bar with animation
class ViewController: UIViewController {
var statusBarHidden: Bool = false {
didSet {
UIView.animate(withDuration: 0.25) { [weak self] in
self?.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
@levantAJ
levantAJ / Debouncer.swift
Last active May 26, 2017 10:10
Remove continuously sequences within a delaying time
//
// A -> B -> C -> D
// `keepFirst`: A
// `keepLast`: D
//
enum DebouncerType {
case keepFirst
case keepLast
}
@levantAJ
levantAJ / Dispatcher.m
Created December 11, 2017 01:19
GCD queues and dispatch method were extracted to properties to facilitate unit testing
#import <XCTest/XCTest.h>
#import "Dispatcher.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface Dispatcher ()
@property(nonatomic, assign) void (*dispatchMethod)(dispatch_queue_t, dispatch_block_t);
@property(nonatomic, strong) dispatch_queue_t queue;
@end
- (void)applicationDidEnterBackground:(UIApplication *)application {
@weakify(self);
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
@strongify(self);
[application endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}
- (void)testApplicationDidEnterBackgroundExpirationHandler {
@levantAJ
levantAJ / CombinationsTest.m
Created February 5, 2018 08:16
Combination Tests
- (void)testCacheSnapshotCombinations {
for (int i = 0; i < 32; i++) {
@autoreleasepool {
//Given:
UIImage *snapshotImage = [UIImage new];
id image = OCMClassMock([UIImage class]);
OCMStub([image imageForView:OCMOCK_ANY]).andReturn(snapshotImage);
if (i & (1 << 0)) {
self.sut.bounds = CGRectMake(0, 0, 123, 456);