Skip to content

Instantly share code, notes, and snippets.

View mikehouse's full-sized avatar

Mikhail Demidov mikehouse

  • Thailand
View GitHub Profile
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'json'
# curl https://localhost:port/test (pass '-k' to accept self signed certificate)
# openssl req -x509 -newkey rsa:4096 -keyout priv.pem -out cert.pem -days 365 -nodes
# cert = OpenSSL::X509::Certificate.new File.read 'cert.pem'
# pkey = OpenSSL::PKey::RSA.new File.read 'priv.pem'
@mikehouse
mikehouse / iOSCornerRadius.swift
Last active July 31, 2018 17:51
iOS 10+ Corner radius
import UIKit
extension UIRectCorner {
func asCornerMask() -> CACornerMask {
var corners: CACornerMask = []
if self.contains(.bottomRight) {
corners.insert(.layerMaxXMaxYCorner)
}
if self.contains(.topRight) {
corners.insert(.layerMaxXMinYCorner)
#import <UIKit/UIKit.h>
@interface ApplicationDelegateProxyType ()
@property (nonnull, nonatomic) NSArray<id <UIApplicationDelegate>> *objects;
@end
@implementation ApplicationDelegateProxyType
extension UIImage {
func gradientImage(_ colors: [UIColor]) -> UIImage? {
guard let source = withRenderingMode(.alwaysTemplate).cgImage else {
return nil
}
// Create gradient
let cgColors = colors.map({ $0.cgColor })
let colors = cgColors as CFArray
let space = CGColorSpaceCreateDeviceRGB()
guard let gradient = CGGradient(colorsSpace: space,
@mikehouse
mikehouse / UIView+Utilities.swift
Created August 21, 2019 17:17
UIView Utilities
import UIKit
extension UIView {
func findFirstSubview(with className: String) -> UIView? {
if String(describing: type(of: self)) == className {
return self
}
for subview in subviews {
if String(describing: type(of: self)) == className {
@mikehouse
mikehouse / Profiler.swift
Last active August 22, 2019 18:56
Swift time profiler
import Foundation
@discardableResult
func measureTime<R>(name: String = #function, body: () -> R) -> R {
#if DEBUG
let startTime = CACurrentMediaTime()
let result: R = body()
let endTime = CACurrentMediaTime()
print("execution time of \(name): \(endTime - startTime)")
return result
@mikehouse
mikehouse / CAAnimations.swift
Created September 16, 2019 20:33
CA Animation
// Do not jump to initila state after animation completed.
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
self.maskShape.path = toPath
CATransaction.commit()
@mikehouse
mikehouse / GCDWebServer+CORS.m
Created October 2, 2019 15:45
GCDWebServer make Cross-Origin Resource Sharing (WKWebView as example)
GCDWebServer *_webServer = [[GCDWebServer alloc] init];
[_webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
GCDWebServerDataResponse *response = [GCDWebServerDataResponse responseWithHTML:@"true"];
[response setValue:@"*" forAdditionalHeader:@"Access-Control-Allow-Origin"];
[response setValue:@"X-Requested-With, Content-Type" forAdditionalHeader:@"Access-Control-Allow-Headers"];
[response setValue:@"GET, POST, OPTIONS" forAdditionalHeader:@"Access-Control-Allow-Methods"];
// Run (https://github.com/wilddylan/WKWebViewWithURLProtocol)
// Be aware it is a private API, it is wisely handled and shouldn't be a prloblem at Apple's code review
[NSURLProtocol wk_registerScheme:@"http"];
// URLProtocol.h
#import <Foundation/Foundation.h>
@interface URLProtocol : NSURLProtocol
let url = Bundle.main.url(forResource: "cat1", withExtension: "jpg")!
let size: CGSize = CGSize(width: 60, height: 90)
let scale = UIScreen.main.scale
// Bind the URL to the object without decoding (will not use memory yet).
let options = [kCGImageSourceShouldCache:false] as CFDictionary
let source = CGImageSourceCreateWithURL(url as CFURL, options)
let dimension = max(size.width, size.height) * scale