Skip to content

Instantly share code, notes, and snippets.

View fethica's full-sized avatar
:octocat:

Fethi fethica

:octocat:
View GitHub Profile
@fethica
fethica / ImageLoader.swift
Last active April 19, 2018 15:00
A simple thumbnail (small) image loader that support caching
class ImageLoader {
private let cache = NSCache<NSString, NSData>()
class func image(for url: URL, completionHandler: @escaping(_ image: UIImage?) -> ()) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
if let data = self.cache.object(forKey: url.absoluteString as NSString) {
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
@fethica
fethica / fetchContentLength.swift
Created January 31, 2018 18:27
[Swift] Get the size (ContentLength) of an http remote file
func fetchContentLength(for url: URL, completionHandler: @escaping (_ contentLength: UInt64?) -> ()) {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard error == nil,
let response = response as? HTTPURLResponse,
let contentLength = response.allHeaderFields["Content-Length"] as? String else {
completionHandler(nil)
@fethica
fethica / isRelativeURL.swift
Created January 31, 2018 18:21
[Swift] is the URL relative or absolute
func isRelative(url: URL) -> Bool {
guard let regex = try? NSRegularExpression(pattern: "^(?:[a-z]+:)?//", options: .caseInsensitive) else { return false }
return regex.firstMatch(in: url.absoluteString, options: [], range: NSRange(location: 0, length: url.absoluteString.count)) == nil
}
let url = URL(string: "https://s3.amazonaws.com/x265.org/video/Tears_400_x265.mp4")!
isRelative(url: url) // false
@fethica
fethica / SwitchToMainThread.swift
Created January 31, 2018 17:35
[Swift] Switch to main thread
struct SwitchToMainThread {
static func with(_ block: @escaping (() -> ())) {
guard !Thread.isMainThread else { block(); return }
DispatchQueue.main.async { SwitchToMainThread.with(block) }
}
}
SwitchToMainThread.with {
// Do UI stuff
}
@fethica
fethica / Units.swift
Last active January 9, 2024 15:42
[Swift] Convert Bytes to Kilobytes to Megabytes to Gigabytes
public struct Units {
public let bytes: Int64
public var kilobytes: Double {
return Double(bytes) / 1_024
}
public var megabytes: Double {
return kilobytes / 1_024
extension UIViewController {
func embed(_ child: UIViewController, animated: Bool = false) {
let duration = animated ? 0.3 : 0.0
child.view.alpha = 0
child.willMove(toParent: self)
self.addChild(child)
self.view.addSubview(child.view)
@fethica
fethica / myViewController.h
Last active September 4, 2015 20:20 — forked from ryanhanwu/myViewController.h
[iOS] Get current location and city name
@interface myViewController: UIViewController <CLLocationManagerDelegate>
CLLocationManager *locationManager;
CLLocation *currentLocation;
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
@fethica
fethica / color.m
Last active August 29, 2015 14:15 — forked from kylefox/color.m
/*
Distributed under The MIT License:
http://opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
@fethica
fethica / chrome
Created September 28, 2014 11:00
Start Google Chrome on Mac with file access
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files