Skip to content

Instantly share code, notes, and snippets.

View haikieu's full-sized avatar

Hai K haikieu

View GitHub Profile
@pczuchaj
pczuchaj / imageDownsampling
Created August 30, 2018 08:26
iOS App memory optimization - image downsampling technique
// Image downsampling technique provides smaller image buffer allocations which leads to memory optimization
extension UIImageView {
func downsampleImage(from imageUrl: URL) -> UIImage {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let imageSource = CGImageSourceCreateWithURL(imageUrl as CFURL, imageSourceOptions) else { return UIImage() }
let maxDimensionInPixels = max(self.bounds.size.width, self.bounds.size.height) * self.traitCollection.displayScale
let downsampleOptions =
[kCGImageSourceCreateThumbnailFromImageAlways: true,
@omarojo
omarojo / imageToPixelBuffer.swift
Created February 23, 2017 23:02
Image To CVPixelBuffer in Swift
var thePixelBuffer : CVPixelBuffer?
let testImage : UIImage = UIImage.init(named: "twdEnds.png")!
self.thePixelBuffer = self.pixelBufferFromImage(image: testImage)
func pixelBufferFromImage(image: UIImage) -> CVPixelBuffer {
@codelynx
codelynx / MTLTexture+Z.swift
Created December 28, 2016 14:58
Piece of Utility code to make CGImage from MTLTexture under (BGRA8Unorm)
//
// MTLTexture+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@darcwader
darcwader / resize-vi.swift
Created August 31, 2016 08:46
Resize Image using vImage
extension UIImage {
func resizeVI(size:CGSize) -> UIImage? {
let cgImage = self.CGImage!
var format = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil,
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.First.rawValue),
version: 0, decode: nil, renderingIntent: CGColorRenderingIntent.RenderingIntentDefault)
var sourceBuffer = vImage_Buffer()
defer {
sourceBuffer.data.dealloc(Int(sourceBuffer.height) * Int(sourceBuffer.height) * 4)
@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
@kristopherjohnson
kristopherjohnson / LowPassFilterSignal.swift
Last active June 10, 2024 05:30
Simple implementation of low-pass filter
struct LowPassFilterSignal {
/// Current signal value
var value: Double
/// A scaling factor in the range 0.0..<1.0 that determines
/// how resistant the value is to change
let filterFactor: Double
/// Update the value, using filterFactor to attenuate changes
mutating func update(newValue: Double) {
@MartinJNash
MartinJNash / gist:e2bb27937711f350aa44
Created June 7, 2014 00:43
Class name to string in Swift
class Person {
class func classString() -> String {
return NSStringFromClass(self)
}
}
Person.classString() // "_TtC11lldb_expr_06Person"
@erichurst
erichurst / US Zip Codes from 2013 Government Data
Created December 9, 2013 23:00
All US zip codes with their corresponding latitude and longitude coordinates. Comma delimited for your database goodness. Source: http://www.census.gov/geo/maps-data/data/gazetteer.html
This file has been truncated, but you can view the full file.
ZIP,LAT,LNG
00601,18.180555, -66.749961
00602,18.361945, -67.175597
00603,18.455183, -67.119887
00606,18.158345, -66.932911
00610,18.295366, -67.125135
00612,18.402253, -66.711397
00616,18.420412, -66.671979
00617,18.445147, -66.559696