This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only for Int raw values | |
class Thread: Object { | |
@objc dynamic var state: State = .ready | |
} | |
@objc enum State: Int { | |
case ready | |
case running | |
case waiting | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CGPoint { | |
func scaled(to size: CGSize) -> CGPoint { | |
return CGPoint(x: self.x * size.width, y: self.y * size.height) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ThrowableOptionalError: Error { | |
case unableToUnwrap | |
} | |
postfix operator +! | |
extension Optional { | |
public func unwrap() throws -> Wrapped { | |
switch self { | |
case .some(let value): |
OlderNewer