Skip to content

Instantly share code, notes, and snippets.

View NikhilManapure's full-sized avatar
🎯
Focusing

Nikhil Manapure NikhilManapure

🎯
Focusing
View GitHub Profile
@NikhilManapure
NikhilManapure / Gif.swift
Last active October 29, 2023 07:31
Create Gif from array of UIImages in Swift 3
import Foundation
import UIKit
import ImageIO
import MobileCoreServices
extension UIImage {
static func animatedGif(from images: [UIImage]) {
let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary
let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): 1.0]] as CFDictionary
@NikhilManapure
NikhilManapure / Overlay.swift
Last active May 7, 2021 04:28
Extension of UIImage to give overlay to an image.
extension UIImage {
func overlayed(with overlay: UIImage) -> UIImage? {
defer {
UIGraphicsEndImageContext()
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
self.draw(in: CGRect(origin: .zero, size: size))
overlay.draw(in: CGRect(origin: .zero, size: size))
if let image = UIGraphicsGetImageFromCurrentImageContext() {
return image
extension UIView {
func embedView(_ view: UIView, inset: UIEdgeInsets = UIEdgeInsets.zero) {
let topConstraint = NSLayoutConstraint(
item: self,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1,
constant: inset.top
@IBDesignable class GridView: UIView {
var numberOfColumns: Int = 2
var numberOfRows: Int = 2
var lineWidth: CGFloat = 1.0
var lineColor: UIColor = UIColor.white
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
// MARK: - KVO
var observedPaths: [String] = []
// Usage - observeKVO(keyPath: #keyPath(camera.inputCamera.whiteBalanceMode))
func observeKVO(keyPath: String) -> Bool {
if shouldObserveKVO {
if self.classForCoder.automaticallyNotifiesObservers(forKey: keyPath) {
observedPaths.append(keyPath)
addObserver(self, forKeyPath: keyPath, options: [.old, .new], context: nil)
class SpinView: UIView {
private var animating: Bool = false
private func spin(with options: UIViewAnimationOptions) {
// this spin completes 360 degrees every 2 seconds
UIView.animate(withDuration: 0.5, delay: 0, options: options, animations: {() -> Void in
self.transform = self.transform.rotated(by: .pi / 2)
}, completion: {(_ finished: Bool) -> Void in
if finished {
if self.animating {
@NikhilManapure
NikhilManapure / NormalizingOrientation.swift
Created June 22, 2017 09:50
Code for making image's orientation as .up
func imageByNormalizingOrientation() -> UIImage {
if imageOrientation == .up {
return self
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(in: CGRect(origin: CGPoint.zero, size: size))
let normalizedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return normalizedImage!
}
@NikhilManapure
NikhilManapure / Blurable.swift
Created July 1, 2017 07:51
Blurable extension with blur and unBlur function
extension UIView {
func blur() {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
}
func unBlur() {
import Foundation
protocol ReusableView: class {
static var identifier: String { get }
}
extension ReusableView where Self: UIView {
static var identifier: String {
return String(describing: self)
}
import Foundation
extension Optional
{
@discardableResult
func ifSome(_ handler: (Wrapped) -> Void) -> Optional {
switch self {
case .some(let wrapped): handler(wrapped); return self
case .none: return self
}