Skip to content

Instantly share code, notes, and snippets.

View StanDimitroff's full-sized avatar
🔨

Stanislav Dimitrov StanDimitroff

🔨
View GitHub Profile
@StanDimitroff
StanDimitroff / Realm+Enum.swift
Last active February 5, 2018 14:16
Using enums in Realm
// Only for Int raw values
class Thread: Object {
@objc dynamic var state: State = .ready
}
@objc enum State: Int {
case ready
case running
case waiting
}
@StanDimitroff
StanDimitroff / Realm+Enum2.swift
Last active February 5, 2018 11:09
Using enums in Realm
final class SortRule: Object {
@objc dynamic var keyRaw: String = ""
@objc dynamic var typeRaw: String = ""
var key: SortKey {
if let key = SortKey.fromRaw(keyRaw) {
return key
}
return .name
@StanDimitroff
StanDimitroff / UIView+UIImage.swift
Created February 16, 2018 10:08
UIImage from UIView
extension UIView {
var asImage: UIImage? {
UIGraphicsBeginImageContextWithOptions(
CGSize(width: self.frame.width, height: self.frame.height), true, 1)
self.layer.render(in: UIGraphicsGetCurrentContext()!)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
return image
}
extension UIImage {
var grayscaled: UIImage? {
guard let openGLContext = EAGLContext(api: .openGLES2) else { return self }
let ciContext = CIContext(eaglContext: openGLContext)
guard let currentFilter = CIFilter(name: "CIPhotoEffectNoir") else { return self }
currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
if let output = currentFilter.outputImage,
let cgImage = ciContext.createCGImage(output, from: output.extent) {
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
@StanDimitroff
StanDimitroff / UIImage+ CINoiseReduction.swift
Last active February 16, 2018 10:38
Image noise reduction
extension UIImage {
var noiseReducted: UIImage? {
guard let openGLContext = EAGLContext(api: .openGLES2) else { return self }
let ciContext = CIContext(eaglContext: openGLContext)
guard let noiseReduction = CIFilter(name: "CINoiseReduction") else { return self }
noiseReduction.setValue(CIImage(image: self), forKey: kCIInputImageKey)
noiseReduction.setValue(0.02, forKey: "inputNoiseLevel")
noiseReduction.setValue(0.40, forKey: "inputSharpness")
@StanDimitroff
StanDimitroff / UIImage+CIPerspectiveCorrection.swift
Last active June 21, 2023 04:49
Image perspective correction
extension UIImage {
var flattened: UIImage? {
let ciImage = CIImage(image: self)!
guard let openGLContext = EAGLContext(api: .openGLES2) else { return nil }
let ciContext = CIContext(eaglContext: openGLContext)
let detector = CIDetector(ofType: CIDetectorTypeRectangle,
context: ciContext,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])!
@StanDimitroff
StanDimitroff / UIImage+Crop.swift
Created February 16, 2018 10:16
Image cropping to AVCaptureVideoPreviewLayer rect
extension UIImage {
func crop(toPreviewLayer layer: AVCaptureVideoPreviewLayer, withRect rect: CGRect) -> UIImage {
let outputRect = layer.metadataOutputRectConverted(fromLayerRect: rect)
var cgImage = self.cgImage!
let width = CGFloat(cgImage.width)
let height = CGFloat(cgImage.height)
let cropRect = CGRect(
x: outputRect.origin.x * width,
y: outputRect.origin.y * height,
width: outputRect.size.width * width,
@StanDimitroff
StanDimitroff / UIImage+Clipp.swift
Last active April 25, 2024 04:52
Image clipping to UIBezierPath
extension UIImage {
func imageByApplyingClippingBezierPath(_ path: UIBezierPath) -> UIImage {
// Mask image using path
guard let let maskedImage = imageByApplyingMaskingBezierPath(path) else { return nil }
// Crop image to frame of path
let croppedImage = UIImage(cgImage: maskedImage.cgImage!.cropping(to: path.bounds)!)
return croppedImage
}
@StanDimitroff
StanDimitroff / CGPoint+Scale.swift
Created February 16, 2018 10:38
CGPoint scale
extension CGPoint {
func scaled(to size: CGSize) -> CGPoint {
return CGPoint(x: self.x * size.width, y: self.y * size.height)
}
}
@StanDimitroff
StanDimitroff / Optional+Throws.swift
Last active March 14, 2018 13:18
Throwable Optionals
public enum ThrowableOptionalError: Error {
case unableToUnwrap
}
postfix operator +!
extension Optional {
public func unwrap() throws -> Wrapped {
switch self {
case .some(let value):