Skip to content

Instantly share code, notes, and snippets.

View Nazar2's full-sized avatar
🎯
Focusing

Developer Nazar2

🎯
Focusing
View GitHub Profile
@yusuke024
yusuke024 / ViewController.swift
Created November 16, 2018 03:15
Recording video with AVAssetWriter
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .notDetermined:
@mminer
mminer / getMACAddress.swift
Created February 1, 2017 19:13
Finds the machine's MAC address.
import Foundation
import IOKit
func getMACAddress() -> String {
let matching = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary
matching[kIOPropertyMatchKey] = ["IOPrimaryInterface": true]
var servicesIterator: io_iterator_t = 0
defer { IOObjectRelease(servicesIterator) }
guard IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &servicesIterator) == KERN_SUCCESS else {
@sooop
sooop / StreamReader.swift
Last active June 23, 2024 22:49
Read a large text file line by line - Swift 3
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
let fileHandle: FileHandle
var buffer: Data
let delimPattern : Data
var isAtEOF: Bool = false
@odrobnik
odrobnik / gist:2751fb3ce32792b8a85d
Last active February 21, 2023 12:06
Swift: create CGPath from attributed string
func appendToPath(path: CGMutablePath)
{
let textPath = CGPathCreateMutable()
let attributedString = NSAttributedString(string: string)
let line = CTLineCreateWithAttributedString(attributedString)
// direct cast to typed array fails for some reason
let runs = (CTLineGetGlyphRuns(line) as [AnyObject]) as! [CTRun]
@schickling
schickling / UIImageFixedOrientationExtension.swift
Last active February 4, 2024 15:00
Extension to fix orientation of an UIImage (Sets orientation to portrait)
extension UIImage {
func fixedOrientation() -> UIImage {
if imageOrientation == UIImageOrientation.Up {
return self
}
var transform: CGAffineTransform = CGAffineTransformIdentity
@jnd-au
jnd-au / groupBy.swift
Last active June 14, 2018 07:51
Swift: Group array values into a dictionary by key mapping (Curryable version)
// Given an array collection (may be a Swift Array or an Objective-C NSArray)
// and a function that optionally maps each element to its corresponding group,
// return a Swift Dictionary of the mapped elements in their groups.
//
// Usage:
// func keyFunc(o: V) -> K {...}
// let grouped = groupBy(keyFunc)([V])
//
// Example:
// func length(s: String) -> Int { return countElements(s) }
@steipete
steipete / UIImage+PSPDFKitAdditions.m
Created August 13, 2011 20:52
Preload UIImage for super-smooth interaction. especially great if you use JPGs, which otherwise produce a noticeable lag on the main thread.
- (UIImage *)pspdf_preloadedImage {
CGImageRef image = self.CGImage;
// make a bitmap context of a suitable size to draw to, forcing decode
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
@kenkam
kenkam / trim_space.c
Created January 21, 2011 17:58
A simple function to trim some space off a string in C
char * trim_space(char *str) {
char *end;
/* skip leading whitespace */
while (isspace(*str)) {
str = str + 1;
}
/* remove trailing whitespace */
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) {
end = end - 1;