Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View viccc's full-sized avatar

Victoria Globa viccc

  • Kyiv, Ukraine
View GitHub Profile

Keybase proof

I hereby claim:

To claim this, I am signing this object:

extension NSBundle {
static func appDisplayName(context: NSFormattingContext) -> String {
if let str = NSBundle.mainBundle().localizedInfoDictionary?["CFBundleDisplayName"] as? String {
return str
}
if let str = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName") as? String {
return str
}
func size(_ imageSize: CGSize, constrainedToSize maxSize: CGSize) -> CGSize {
if maxSize.width > 0 && maxSize.height > 0 && (maxSize.width < imageSize.width || maxSize.height < imageSize.height)
{
let constrainedSize: CGSize
let constrainedHeight = min(imageSize.height, maxSize.height)
let scaleProportionalToHeight = constrainedHeight / imageSize.height
let sizeProportionalToHeight = CGSize(width: round(imageSize.width * scaleProportionalToHeight), height: constrainedHeight)
let constrainedWidth = min(imageSize.width, maxSize.width)
@viccc
viccc / UniqueFilename.swift
Last active January 22, 2016 17:00
Utility functions to create a unique file name in a given directory, with shorter helper functions for Documents, ApplicationSupport, tmp, Caches
extension NSFileManager {
class func uniqueDocumentFileURLWithBaseName(baseName: String) throws -> NSURL {
let directoryUrl = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
return try directoryUrl.URLByAppendingUniqueFileNameWithBaseName(baseName)
}
class func uniqueAppSupportFileURL(inSubdirectory subdirectory: String, uniqueFileBaseName baseName: String) throws -> NSURL {
let appSupportUrl = try NSFileManager.defaultManager().URLForDirectory(.ApplicationSupportDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let directoryUrl: NSURL
if subdirectory.isEmpty {
@viccc
viccc / ShakeToDebug.swift
Created January 10, 2016 14:34
Shake to execute some code for debugging. To be inserted into view controller.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
#if DEBUG
// Shake to debug
becomeFirstResponder()
#endif
// ...
}
func randomInt(range: Range<Int>) -> Int {
let base = range.endIndex - range.startIndex
let r = arc4random_uniform(UInt32(base))
let n = Int(r) + range.startIndex
return n
}
@viccc
viccc / AdjustImageOrientation.swift
Created November 11, 2015 01:42
Re-render UIImage with .Up orientation if it has different orientation. Swift.
func normalizeImageOrientationIfNeeded(image: UIImage) -> UIImage {
if image.imageOrientation == .Up {
return image
}
let size = image.size
let scale = image.scale
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
@viccc
viccc / CreateEmptyImage.swift
Created November 9, 2015 21:51
Create an empty UIImage of a given size, optionally filled with a given color. Swift.
func imageWithPixelSize(size: CGSize, filledWithColor color: UIColor = UIColor.clearColor(), opaque: Bool = false) -> UIImage {
return imageWithSize(size, filledWithColor: color, scale: 1.0, opaque: opaque)
}
func imageWithSize(size: CGSize, filledWithColor color: UIColor = UIColor.clearColor(), scale: CGFloat = 0.0, opaque: Bool = false) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
color.set()
UIRectFill(rect)
@viccc
viccc / DownscaleImage.swift
Last active January 9, 2024 03:13
Resize+crop image to a smaller size (using scale aspect fill logic), if image is indeed larger than required size. In Swift.
func downscaleImage(image: UIImage, toPixelSize targetSize: CGSize) -> UIImage {
return downscaleImage(image, toSize: targetSize, scale: 1.0)
}
func downscaleImage(image: UIImage, toSize targetSize: CGSize, scale: CGFloat = 0.0) -> UIImage {
let actualScaleFactor = (scale == 0.0) ? UIScreen.mainScreen().scale : scale
let size = image.size
let imageScaleFactor = image.scale
let imagePixelSize = CGSizeMake(size.width * imageScaleFactor, size.height * imageScaleFactor)
@viccc
viccc / RBResizer.swift
Last active May 3, 2016 23:10 — forked from HamptonMakes/RBResizer.swift
Swift Image Resizer
//
// RBResizer.swift
// Locker
//
// Created by Hampton Catlin on 6/20/14.
// Copyright (c) 2014 rarebit. All rights reserved.
//
import UIKit