Skip to content

Instantly share code, notes, and snippets.

View alankarmisra's full-sized avatar

Alankar Misra alankarmisra

  • Full-stack Developer | Digital Nomad
  • Mumbai, India
  • LinkedIn in/alankarmisra
View GitHub Profile
@alankarmisra
alankarmisra / AspectRatioScaledToWidthFilter.swift
Created July 28, 2016 19:19
Alamofire Filter to scale image to fit a specified width while respecting aspect ratio for height. Unlike UIViewContentMode.ScaleAspectFit this won't leave whitespace on the top and bottom of a horizontal image and it *will* scale images smaller than the ImageView. Especially useful for UIImageView's that span the entire screen width.
//
// AspectRatioScaledToWidthFilter.swift
// Usage example where a UIImageView spans the screen width:
// let url = NSURL(string:"http://www.yourcompany.com/someimage.jpg")
// let scaleFilter = AspectRatioScaledToWidthFilter(width: UIScreen.mainScreen().bounds.width)
// myImageView.af_setImageWithURL(url, placeholderImage: nil, filter: scaleFilter)
//
// Created by Alankar Misra on 7/29/16.