Skip to content

Commit

Permalink
Updated for Xcode 6 Beta 7
Browse files Browse the repository at this point in the history
  • Loading branch information
melvitax committed Sep 2, 2014
1 parent 1035500 commit 8c1b580
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 92 deletions.
2 changes: 1 addition & 1 deletion AF+Image+Helper.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AF+Image+Helper'
s.version = '1.01'
s.version = '1.02'
s.platform = :ios
s.license = 'MIT'
s.summary = 'Convenience extension for UIImage and UIImageView in Swift'
Expand Down
33 changes: 15 additions & 18 deletions AF+Image+Helper/AF+Image+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// AF+Image+Extension.swift
//
// Version 1.01
// Version 1.02
//
// Created by Melvin Rivera on 7/5/14.
// Copyright (c) 2014 All Forces. All rights reserved.
Expand Down Expand Up @@ -32,7 +32,7 @@ extension UIImage {
return StaticSharedCache.sharedCache!
}

//#pragma mark - Image from solid color
// MARK: Image from solid color
convenience init(color:UIColor, size:CGSize = CGSizeMake(10, 10) )
{
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
Expand All @@ -44,7 +44,7 @@ extension UIImage {
UIGraphicsEndImageContext()
}

//#pragma mark - Image from gradient colors
// MARK: Image from gradient colors
convenience init(gradientColors:[UIColor], size:CGSize = CGSizeMake(10, 10) )
{
UIGraphicsBeginImageContextWithOptions(size, false, 0)
Expand Down Expand Up @@ -79,7 +79,7 @@ extension UIImage {
return image;
}

//#pragma mark - Image with Text
// MARK: Image with Text
convenience init(text: String, font: UIFont = UIFont.systemFontOfSize(18), color: UIColor = UIColor.whiteColor(), backgroundColor: UIColor = UIColor.grayColor(), size:CGSize = CGSizeMake(100, 100), offset: CGPoint = CGPoint(x: 0, y: 0))
{
UIGraphicsBeginImageContextWithOptions(size, false, 0)
Expand All @@ -95,7 +95,7 @@ extension UIImage {
UIGraphicsEndImageContext()
}

//# pragma mark - Image from uiview
// MARK: Image from uiview
convenience init(fromView view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
//view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
Expand All @@ -104,7 +104,7 @@ extension UIImage {
UIGraphicsEndImageContext()
}

//#pragma mark - Image with Radial Gradient
// MARK: Image with Radial Gradient
// Render a radial background
// Originally from: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html
convenience init(startColor: UIColor, endColor: UIColor, radialGradientCenter: CGPoint = CGPoint(x: 0.5, y: 0.5), radius:Float = 0.5, size:CGSize = CGSizeMake(100, 100))
Expand Down Expand Up @@ -135,7 +135,7 @@ extension UIImage {
UIGraphicsEndImageContext()
}

//#pragma mark - Alpha
// MARK: Alpha

// Returns true if the image has an alpha layer
func hasAlpha() -> Bool
Expand Down Expand Up @@ -196,14 +196,11 @@ extension UIImage {
var transparentImage = UIImage(CGImage: CGImageCreateWithMask(CGBitmapContextCreateImage(context), imageRefWithPadding(padding, size: rect.size)))
return transparentImage
}

//#pragma mark - Private helper methods



// Creates a mask that makes the outer edges transparent and everything else opaque
// The size must include the entire mask (opaque part + transparent border)
// The caller is responsible for releasing the returned reference by calling CGImageRelease
func imageRefWithPadding(padding: CGFloat, size:CGSize) -> CGImageRef
private func imageRefWithPadding(padding: CGFloat, size:CGSize) -> CGImageRef
{
// Build a context that's the same dimensions as the new size
let colorSpace = CGColorSpaceCreateDeviceGray()
Expand All @@ -220,8 +217,8 @@ extension UIImage {
return maskImageRef
}

//#pragma mark - Crop


// MARK: Crop

func crop(bounds: CGRect) -> UIImage
{
Expand All @@ -237,7 +234,7 @@ extension UIImage {
return crop(insetRect)
}

//#pragma mark - Resize
// MARK: Resize

func resize(size:CGSize, contentMode: UIImageContentMode = .ScaleToFill) -> UIImage
{
Expand Down Expand Up @@ -283,7 +280,7 @@ extension UIImage {
}


// #pragma mark - Corner Radius
// MARK: Corner Radius

func roundCorners(cornerRadius:CGFloat) -> UIImage
{
Expand Down Expand Up @@ -341,7 +338,7 @@ extension UIImage {
return cropToSquare().roundCorners(shortest/2, border: border, color: color)
}

// #pragma mark - Border
// MARK: Border

func applyBorder(border:CGFloat, color:UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
Expand All @@ -364,7 +361,7 @@ extension UIImage {
return image
}

//#pragma mark - Image From URL
// MARK: Image From URL

class func imageFromURL(url: String, placeholder: UIImage, shouldCacheImage: Bool = true, closure: (image: UIImage?) -> ()) -> UIImage?
{
Expand Down
2 changes: 1 addition & 1 deletion AF+Image+Helper/AF+ImageVIew+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension UIImageView {
if fadeIn {
let crossFade = CABasicAnimation(keyPath: "contents")
crossFade.duration = 0.5
crossFade.fromValue = self.image.CGImage!
crossFade.fromValue = self.image?.CIImage
crossFade.toValue = image!.CGImage
self.layer.addAnimation(crossFade, forKey: "")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>2AE87E7C-FBA3-4468-BBE7-797E330C96C2</string>
<key>IDESourceControlProjectName</key>
<string>Demo AF+Image+Helper</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>8F037519E7CC82DD9D474EF3CEF9898FE7961B03</key>
<string>https://github.com/melvitax/AFImageHelper.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>Demo AF+Image+Helper/Demo AF+Image+Helper.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>8F037519E7CC82DD9D474EF3CEF9898FE7961B03</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/melvitax/AFImageHelper.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>8F037519E7CC82DD9D474EF3CEF9898FE7961B03</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>8F037519E7CC82DD9D474EF3CEF9898FE7961B03</string>
<key>IDESourceControlWCCName</key>
<string>AF+Image+Helper</string>
</dict>
</array>
</dict>
</plist>
Binary file not shown.
139 changes: 69 additions & 70 deletions Demo AF+Image+Helper/Demo AF+Image+Helper/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import UIKit
import CoreImage

enum ImageType: Int {
case FromURL, SolidColor, GradientColor, GradientOverlay, RadialGradient, Text, Circle, CirclePlusBorder, RoundCorners, CropPlusResize, FromView
struct CellItem {
let text: String
let image: UIImage
}

class Cell: UICollectionViewCell {
Expand All @@ -20,99 +21,97 @@ class Cell: UICollectionViewCell {

class ViewController: UICollectionViewController {

let sections = ["Web Image", "Colors and Gradients", "Text", "Rounded Edges & Borders", "Cropping", "Screenshot"]
let items: [[ImageType]] = [
[.FromURL],
[.SolidColor, .GradientColor, .GradientOverlay, .RadialGradient],
[.Text],
[.Circle, .CirclePlusBorder, .RoundCorners],
[.CropPlusResize],
[.FromView]
]
let imageWidth = 140
let imageHeight = 140

var sections = [String]()
var items = [[CellItem]]()

override func viewDidLoad() {

super.viewDidLoad()

// Colors & Gradients
sections.append("Colors & Gradients")
items.append([
CellItem(text: "Solid Color", image: UIImage(color: UIColor(red: 0, green: 0.502, blue: 1, alpha: 1), size: CGSize(width: imageWidth, height: imageHeight))),
CellItem(text: "Gradient Color", image: UIImage(gradientColors: [UIColor(red: 0.808, green: 0.863, blue: 0.902, alpha: 1), UIColor(red: 0.349, green: 0.412, blue: 0.443, alpha: 1)], size: CGSize(width: imageWidth, height: imageHeight))),
CellItem(text: "Gradient Overlay", image: UIImage(named: "beach").applyGradientColors([UIColor(red: 0.996, green: 0.769, blue: 0.494, alpha: 1), UIColor(red: 0.969, green: 0.608, blue: 0.212, alpha: 0.2)])),
CellItem(text: "Radial Gradient", image: UIImage(startColor: UIColor(red: 0.996, green: 1, blue: 1, alpha: 1), endColor: UIColor(red: 0.627, green: 0.835, blue: 0.922, alpha: 1), radialGradientCenter: CGPoint(x: 0.5, y: 0.5), radius: 0.5, size: CGSize(width: imageWidth, height: imageHeight)))
])

// Text
sections.append("Text")
items.append([
CellItem(text: "Text Image", image: UIImage(text: "M", font: UIFont.systemFontOfSize(64), color: UIColor.whiteColor(), backgroundColor: UIColor.redColor(), size: CGSize(width: imageWidth, height: imageHeight), offset: CGPoint(x: 0, y: 30)))
])

// Rounded Edges & Borders
sections.append("Rounded Edges & Borders")
items.append([
CellItem(text: "Circle", image: UIImage(named: "beach").roundCornersToCircle()),
CellItem(text: "Circle + Border", image: UIImage(named: "beach").roundCornersToCircle(border: 60, color: UIColor.grayColor())),
CellItem(text: "Round Corners", image: UIImage(named: "beach").roundCorners(12))
])

// Cropping
sections.append("Cropping")
items.append([
CellItem(text: "Crop + Resize", image: UIImage(named: "beach").crop(CGRect(x: 40, y: 40, width: 320, height: 100)).applyPadding(6))
])

// Screenshot
sections.append("Screenshot")
items.append([
CellItem(text: "From View", image: UIImage(fromView: self.view).resize(CGSize(width: imageWidth, height: imageHeight), contentMode: .ScaleAspectFill))
])

// Web Image
sections.append("Web Image")
items.append([
CellItem(text: "From URL", image: UIImage(color: UIColor.redColor()))
])

}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int
{
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return items.count
}

override func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView:UICollectionReusableView! = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as UICollectionReusableView
let textLabel = headerView.viewWithTag(1) as UILabel
textLabel.text = sections[indexPath.section]
return headerView
}

override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return items[section].count
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cellID = "Cell"
let cell:Cell! = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as Cell

let item: ImageType = items[indexPath.section][indexPath.item]

switch item {

case .GradientColor:
cell.imageView.image = UIImage(gradientColors: [UIColor(red: 0.808, green: 0.863, blue: 0.902, alpha: 1), UIColor(red: 0.349, green: 0.412, blue: 0.443, alpha: 1)], size: CGSize(width: imageWidth, height: imageHeight))
cell.textLabel.text = "Gradient Color"

case .GradientOverlay:
cell.imageView.image = UIImage(named: "beach").applyGradientColors([UIColor(red: 0.996, green: 0.769, blue: 0.494, alpha: 1), UIColor(red: 0.969, green: 0.608, blue: 0.212, alpha: 0.2)])
cell.textLabel.text = "Gradient Overlay"

case .RadialGradient:
cell.imageView.image = UIImage(startColor: UIColor(red: 0.996, green: 1, blue: 1, alpha: 1), endColor: UIColor(red: 0.627, green: 0.835, blue: 0.922, alpha: 1), radialGradientCenter: CGPoint(x: 0.5, y: 0.5), radius: 0.5, size: CGSize(width: imageWidth, height: imageHeight))
cell.textLabel.text = "Radial Gradient"

case .Text:
cell.imageView.image = UIImage(text: "M", font: UIFont.systemFontOfSize(64), color: UIColor.whiteColor(), backgroundColor: UIColor.redColor(), size: CGSize(width: imageWidth, height: imageHeight), offset: CGPoint(x: 0, y: 30))
cell.textLabel.text = "Text Image"

case .Circle:
cell.imageView.image = UIImage(named: "beach").roundCornersToCircle()
cell.textLabel.text = "Circle"

case .CirclePlusBorder:
cell.imageView.image = UIImage(named: "beach").roundCornersToCircle(border: 60, color: UIColor.grayColor())
cell.textLabel.text = "Circle + Border"

case .RoundCorners:
cell.imageView.image = UIImage(named: "beach").roundCorners(12)
cell.textLabel.text = "Round Corners"

case .CropPlusResize:
cell.imageView.image = UIImage(named: "beach").crop(CGRect(x: 40, y: 40, width: 320, height: 100)).applyPadding(6)
cell.textLabel.text = "Crop + Resize"

case .FromView:
cell.imageView.image = UIImage(fromView: self.view).resize(CGSize(width: imageWidth, height: imageHeight), contentMode: .ScaleAspectFill)
cell.textLabel.text = "From View"

case .FromURL:
cell.imageView.imageFromURL("https://c2.staticflickr.com/4/3212/3130969018_ed7516c288_n.jpg", placeholder: UIImage(color: UIColor.redColor(), size: CGSize(width: imageWidth, height: imageHeight)), fadeIn: true) {
(image: UIImage?) in
if image != nil {
cell.imageView.image = image!
}
}
cell.textLabel.text = "From URL"

default:
cell.imageView.image = UIImage(color: UIColor(red: 0, green: 0.502, blue: 1, alpha: 1), size: CGSize(width: imageWidth, height: imageHeight))
cell.textLabel.text = "Solid Color"

let item: CellItem = items[indexPath.section][indexPath.item]
cell.textLabel.text = item.text
if (indexPath.section == items.count-1) {
cell.imageView.imageFromURL("https://c2.staticflickr.com/4/3212/3130969018_ed7516c288_n.jpg", placeholder: item.image, fadeIn: true) {
(image: UIImage?) in
if image != nil {
cell.imageView.image = image!
}
}
} else {
cell.imageView.image = item.image
}

return cell
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
AF+Image+Helper
AF+Image+Helper 1.02
=============================

Convenience extension for UIImage and UIImageView in Swift
A collection of extensions for handling image creation from colors and gradients, cropping, scaling and fetching from the web with support for caching.

Tested with Xcode 6 Beta 6
Tested with Xcode 6 Beta 7

![Sample Project Screenshot](Screenshot.png?raw=true "Sample Project Screenshot")

Expand Down

0 comments on commit 8c1b580

Please sign in to comment.