Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
let string = "This is a string with an \u{2014} unicode character &mdash"
// Load video
// Note: Needs to be a supported AVPlayer format, in this case MP4 (could also be M4V etc.)
if let filePath = Bundle.main.path(forResource: "video", ofType: "mp4") {
// url
let fileUrl = URL(fileURLWithPath: filePath)
// Player
let player = AVPlayer(url: fileUrl)
extension UIImage {
class func imageWithLayer(layer: CALayer) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, layer.isOpaque, 0.0)
layer.render(in: UIGraphicsGetCurrentContext()!)
guard let img = UIGraphicsGetImageFromCurrentImageContext() else {
return nil
}
UIGraphicsEndImageContext()
@michaelevensen
michaelevensen / EventKit.swift
Last active March 31, 2025 01:27
Steps for fetching events from EventKit. NOTE! For iOS10+ remember to add: Privacy – Calendars Usage Description in .plist
// Helper function for showing UIAlert prompts
func showMessagePrompt(_ title: String, message: String) {
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
/********************/
@michaelevensen
michaelevensen / RoundedView.swift
Last active November 17, 2016 10:39
Simple IBDesignable template for a UIView with customizable cornerRadius, strokeWidth and strokeColor.
@IBDesignable
class RoundedView: UIView {
@IBInspectable var roundedValue: CGFloat = 0.0 {
didSet {
layer.cornerRadius = roundedValue
}
}
@IBInspectable var strokeWidth: CGFloat = 0.0 {
@michaelevensen
michaelevensen / Map.swift
Created January 3, 2017 18:18
Simple Map example.
// Charger locations array of touples (Double, Double)
let chargeLocations = stations.map { ($0.location.latitude, $0.location.longitude) }
// Returns an array of touples exctracted from the location value of each array element.
@michaelevensen
michaelevensen / VideoEmbed.swift
Last active February 16, 2017 12:20
Video embed example with AVKit and AVFoundation. Loops video ∞.
import AVKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var videoOverlayView: UIView!
@IBOutlet weak var videoView: UIView!
var player: AVPlayer?
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>michaelevensen.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
@michaelevensen
michaelevensen / mapViewController.m
Created February 15, 2017 10:27 — forked from ryanhanwu/mapViewController.m
Calculate Google Map length in meters with zoom level in #Objective-C (converted from #JavaScript) #iOS
- (void)mapView:(GMSMapView*)mapView idleAtCameraPosition:(GMSCameraPosition*)position
{
CLLocationCoordinate2D topLeft = mapView.projection.visibleRegion.farLeft;
CLLocationCoordinate2D bottomLeft = mapView.projection.visibleRegion.nearLeft;
double lat = fabs(topLeft.latitude - bottomLeft.latitude);
double mpp = cos(lat * M_PI / 180) * 2 * M_PI * 6378137 / (256 * pow(2, mapView.camera.zoom));
double distance = mpp * mapView.frame.size.width;
[[SearchManager shareInstance] distance: distance];
}
// Conform to Hashable
extension ChargeConnector: Hashable {
var hashValue: Int {
return id.hashValue ^ id.hashValue
}
static func == (lhs: ChargeConnector, rhs: ChargeConnector) -> Bool {
return lhs.id == rhs.id
}