Skip to content

Instantly share code, notes, and snippets.

View antranapp's full-sized avatar

An Tran antranapp

View GitHub Profile
@SangsooNam
SangsooNam / gh-pages-deploy.sh
Created January 6, 2019 23:11
Script to deploy a target directory to `gh-pages` branch.
#!/bin/bash
directory=_site
branch=gh-pages
build_command() {
jekyll build
}
echo -e "\033[0;32mDeleting old content...\033[0m"
rm -rf $directory
@shalyf
shalyf / CVPixelBuffer(vImage).swift
Last active January 6, 2024 20:46
CVPixelBuffer通过vImage转换成CGImage
import Accelerate.vImage
func getImageBuffer(from pixelBuffer: CVPixelBuffer) -> vImage_Buffer? {
var buffer = vImage_Buffer()
let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.first.rawValue)
var cgFormat = vImage_CGImageFormat(bitsPerComponent: 8,
bitsPerPixel: 32,
colorSpace: nil,
bitmapInfo: bitmapInfo,
version: 0,
@cprovatas
cprovatas / Data+PrettyPrint.swift
Created May 23, 2018 15:52
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
@ole
ole / core-data-backup.swift
Last active January 1, 2024 16:52
How to make a copy of a Core Data SQLite database. See https://oleb.net/blog/2018/03/core-data-sqlite-backup/ for more.
import CoreData
import Foundation
/// Safely copies the specified `NSPersistentStore` to a temporary file.
/// Useful for backups.
///
/// - Parameter index: The index of the persistent store in the coordinator's
/// `persistentStores` array. Passing an index that doesn't exist will trap.
///
/// - Returns: The URL of the backup file, wrapped in a TemporaryFile instance
@hooman
hooman / WeakReference.swift
Last active December 29, 2022 09:06
A set of utilities for working with weak references.
//: Playground - noun: a place where people can play
/// A protocol to provide an abstraction of types that hold a weak reference to a target object.
///
/// It is defined because a single generic implementation cannot support existentials, as they
/// do not conform to themselves or `AnyObject`. Most of its API is defined by protocol extensions
/// to makes it easier to create existential wrapper `struct`s or `final class`es.
///
/// Here is an example protocol and the corresponding weak reference
@norsez
norsez / MovieWriter.swift
Created May 11, 2017 04:13 — forked from isthisjoe/MovieWriter.swift
Swift 3 | macOs | Write NSImage(s) to a movie file. Modified from http://stackoverflow.com/a/36297656/1275125
import AppKit
import AVFoundation
class MovieWriter: NSObject {
func writeImagesAsMovie(_ allImages: [NSImage], videoPath: String, videoSize: CGSize, videoFPS: Int32) {
// Create AVAssetWriter to write video
guard let assetWriter = createAssetWriter(videoPath, size: videoSize) else {
print("Error converting images to video: AVAssetWriter not created")
return
}
@actuino
actuino / bt_speaker-raspberry_pi-zero_w.md
Last active December 30, 2024 06:10
Setting up a Bluetooth Speaker from the command line on a raspberry Pi Zero W

The setup of a bluetooth speaker on a Pi Zero W is pretty touchy.

Please get in touch via Twitter @actuino or http://www.actuino.fr/ if you've got comments or improvements to this quick draft.

First checks

  • Use a solid power source
  • check the speaker works on another hardware (android phone f.i.)
  • make sure you've updated your Raspbian, install and run rpi-update just in case.
@andymatuschak
andymatuschak / States-v3.md
Last active May 12, 2025 06:11
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
var copiedPixelBuffer : CVPixelBuffer?
CVPixelBufferCreate(
nil,
CVPixelBufferGetWidth(pixelBuffer),
CVPixelBufferGetHeight(pixelBuffer),
CVPixelBufferGetPixelFormatType(pixelBuffer),
nil,
&copiedPixelBuffer)
let duration = CMTime( value:10, timescale:600, flags: CMTimeFlags(rawValue: 0), epoch:0)
let presentationTimeStamp = CMTime( value:0, timescale:600, flags:CMTimeFlags(rawValue: 0), epoch:0)
var timingInfo = CMSampleTimingInfo( duration:duration, presentationTimeStamp:presentationTimeStamp, decodeTimeStamp:kCMTimeInvalid )
// let potentialBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer)
// var imageBufferCopy = UnsafeMutablePointer<CVImageBuffer>.alloc(1)
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
fatalError("Sample Buffer did not contain a pixel buffer")
}