Skip to content

Instantly share code, notes, and snippets.

View eleev's full-sized avatar

Astemir Eleev eleev

View GitHub Profile
@eleev
eleev / URLForPHAsset.swift
Last active February 17, 2024 23:56
Getting URL for PHAsset (Swift 3.0)
func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})
@eleev
eleev / Auto-layout-keyboard-adjustment.md
Created March 17, 2017 09:08 — forked from dlo/Auto-layout-keyboard-adjustment.md
How to adjust a view's height with Auto Layout when a keyboard appears or disappears in iOS 7.

This gist outlines how to resize a view when a keyboard appears using Auto Layout (there are a bunch of code samples out there that manually adjust the view's frame, but that's just so 2013). The method I outline below works universally on both iPhone and iPad, portrait and landscape, and is pretty darn simple.

Setting Up

The first thing to do is to define our containing view controller, the view, and the bottom constraint that we'll use to adjust its size.

Here's HeightAdjustingViewController.h. We don't need to expose any public properties, so it's pretty bare.

@eleev
eleev / UIScreen+InterfaceOrientation.swift
Last active March 24, 2017 11:19
This extnesion allows to get UIInterfaceOrientation from UIScreen. It is useful for cases when by some reasons UIStatusBar and UIDevice.current.orientation are not available e.g. iMessage extension target in iOS 10.
extension UIScreen {
var orientation: UIInterfaceOrientation {
let point = coordinateSpace.convert(CGPoint.zero, to: fixedCoordinateSpace)
if point == CGPoint.zero {
return .portrait
} else if point.x != 0 && point.y != 0 {
return .portraitUpsideDown
} else if point.x == 0 && point.y != 0 {
return .landscapeLeft
@eleev
eleev / Date+String.swift
Last active March 24, 2017 11:19
Extension for Date class that provides easy-to-use feature for convering date to string. Swift 3
extension Date {
struct Formatter {
static let shortDate = DateFormatter(dateStyle: .short)
static let longDate = DateFormatter(dateStyle: .long)
static let fullDate = DateFormatter(dateStyle: .full)
}
var shortDate: String { return Formatter.shortDate.string(from: self) }
var longDate: String { return Formatter.longDate.string(from: self) }
var fullDate: String { return Formatter.fullDate.string(from: self) }
@eleev
eleev / UIImage+SolidColor.swift
Created March 24, 2017 11:18
Extension for creating UIImage from a UIColor. Swift 3
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
@eleev
eleev / UIApplication+InternetAvailability.swift
Created March 24, 2017 11:22
Class-level extension for UIApplication for checking internet connectivity. Swift 3
extension UIApplication {
class func isInternetAvailable() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
@eleev
eleev / UIImage+LandscapeCameraCaptureOrientationFix.swift
Created March 24, 2017 12:57
This extension for UIImage fixes image orientation for cases when the iamge was captured using AVFoundation in landscape interface orientation. Swift 3
func landscapeCameraCaptureOrientationFix(for interfaceOrinetation: UIInterfaceOrientation) -> UIImage? {
var imageOrientation: UIImageOrientation!
var shouldOrient: Bool = false
if interfaceOrinetation == .landscapeRight {
imageOrientation = UIImageOrientation.left
shouldOrient = !shouldOrient
} else if interfaceOrinetation == .landscapeLeft {
imageOrientation = UIImageOrientation.right
@eleev
eleev / UIImage+TiffOrientationFix.swift
Created March 24, 2017 13:08
This calss-level UIImage extension aims to help identifying raw value for UIImageOrientation. This may be useful when warking with CGImage or CIImage classes. Swift 3
extension UIImage {
class func imageOrientationToTiffOrientation(_ value: UIImageOrientation) -> Int32 {
switch (value) {
case .up:
return 1
case .down:
return 3
case .left:
return 8
case .right:
@eleev
eleev / UIImage+Resize.swift
Created March 26, 2017 11:44
Class-level extension for UIImage that allow to resize input image based on expected image width or/and height. Swift 3
extension UIImage {
class func resize(_ image: UIImage, newWidth: CGFloat) -> UIImage? {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
@eleev
eleev / DispatchQueue+DispatchOnce.swift
Last active March 30, 2018 14:13
DispatchOnce + Swift 3 (and above).swift
public extension DispatchQueue {
// MARK: - Properties
private static var _onceTracker = [String]()
// MARK: - Methods
/// Executes a block of code, associated with a unique token, only once. The code is thread safe and will onle execute the code once even in the presence of multithreaded calls.