Skip to content

Instantly share code, notes, and snippets.

View eleev's full-sized avatar

Astemir Eleev eleev

View GitHub Profile
@eleev
eleev / UIImage+Inset.swift
Created August 5, 2022 09:52
UIImage Inset - blank space around a UIImage
import UIKit
public extension UIImage {
func image(uniformInset inset: CGFloat) -> UIImage? {
image(with:
UIEdgeInsets(
top: inset,
left: inset,
bottom: inset,
right: inset
@eleev
eleev / gitrename.sh
Created May 9, 2019 07:51
Git branch renaming [local & remote]
git branch -m old_branch new_branch # First, rename branch locally
git push origin :old_branch # Then, delete the outdated branch
git push --set-upstream origin new_branch # And finally, push the new branch. Also, the new remote needs to be tracked, that is why we set set local branch to do so.
@eleev
eleev / iOS UIFont Names (Raw).swift
Created July 30, 2018 07:19
Use the following reference to properly construct UIFont instances with valid font names
UIFont: family Thonburi
UIFont: font Thonburi-Bold
UIFont: font Thonburi
UIFont: font Thonburi-Light
UIFont: family Khmer Sangam MN
UIFont: font KhmerSangamMN
UIFont: family Snell Roundhand
UIFont: font SnellRoundhand-Black
UIFont: font SnellRoundhand-Bold
UIFont: font SnellRoundhand
@eleev
eleev / KeyCodesMacOS.swift
Created July 29, 2018 15:54
The code snippet contains key codes for `macOS` keyboard
import Carbon
struct KeyCode {
static let a = UInt16(kVK_ANSI_A)
static let b = UInt16(kVK_ANSI_B)
static let c = UInt16(kVK_ANSI_C)
static let d = UInt16(kVK_ANSI_D)
static let e = UInt16(kVK_ANSI_E)
static let f = UInt16(kVK_ANSI_F)
static let g = UInt16(kVK_ANSI_G)
@eleev
eleev / DispatchQueue+DispatchOnce.swift
Last active March 30, 2018 14:13
DispatchOnce + Swift 3 (and above).swift
public extension DispatchQueue {
// MARK: - Properties
private static var _onceTracker = [String]()
// MARK: - Methods
/// Executes a block of code, associated with a unique token, only once. The code is thread safe and will onle execute the code once even in the presence of multithreaded calls.
@eleev
eleev / UIImage+Resize.swift
Created March 26, 2017 11:44
Class-level extension for UIImage that allow to resize input image based on expected image width or/and height. Swift 3
extension UIImage {
class func resize(_ image: UIImage, newWidth: CGFloat) -> UIImage? {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
@eleev
eleev / UIImage+TiffOrientationFix.swift
Created March 24, 2017 13:08
This calss-level UIImage extension aims to help identifying raw value for UIImageOrientation. This may be useful when warking with CGImage or CIImage classes. Swift 3
extension UIImage {
class func imageOrientationToTiffOrientation(_ value: UIImageOrientation) -> Int32 {
switch (value) {
case .up:
return 1
case .down:
return 3
case .left:
return 8
case .right:
@eleev
eleev / UIImage+LandscapeCameraCaptureOrientationFix.swift
Created March 24, 2017 12:57
This extension for UIImage fixes image orientation for cases when the iamge was captured using AVFoundation in landscape interface orientation. Swift 3
func landscapeCameraCaptureOrientationFix(for interfaceOrinetation: UIInterfaceOrientation) -> UIImage? {
var imageOrientation: UIImageOrientation!
var shouldOrient: Bool = false
if interfaceOrinetation == .landscapeRight {
imageOrientation = UIImageOrientation.left
shouldOrient = !shouldOrient
} else if interfaceOrinetation == .landscapeLeft {
imageOrientation = UIImageOrientation.right
@eleev
eleev / UIApplication+InternetAvailability.swift
Created March 24, 2017 11:22
Class-level extension for UIApplication for checking internet connectivity. Swift 3
extension UIApplication {
class func isInternetAvailable() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}