Skip to content

Commit

Permalink
added ability to use seed
Browse files Browse the repository at this point in the history
wvabrinskas committed Aug 16, 2017
1 parent c4a2443 commit d56a62a
Showing 6 changed files with 93 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Avatar.podspec
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "Avatar"
s.version = "1.0.1"
s.version = "1.0.2"
s.summary = "Randomly generated user Avatar images"

# This description is used to generate tags and improve search results.
12 changes: 8 additions & 4 deletions Avatar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -7,15 +7,17 @@
objects = {

/* Begin PBXBuildFile section */
50017B3C1F448F6D00058FF6 /* Avatar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 50017B3B1F448F6D00058FF6 /* Avatar.swift */; };
50017B401F44A20500058FF6 /* AvatarSeed.swift in Headers */ = {isa = PBXBuildFile; fileRef = 50017B3F1F44A20500058FF6 /* AvatarSeed.swift */; };
503987971EF085630032142F /* Avatar.h in Headers */ = {isa = PBXBuildFile; fileRef = 503987951EF085630032142F /* Avatar.h */; settings = {ATTRIBUTES = (Public, ); }; };
5039879E1EF0857D0032142F /* Avatar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5039879D1EF0857D0032142F /* Avatar.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
50017B3B1F448F6D00058FF6 /* Avatar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Avatar.swift; sourceTree = "<group>"; };
50017B3F1F44A20500058FF6 /* AvatarSeed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarSeed.swift; sourceTree = "<group>"; };
503987921EF085630032142F /* Avatar.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Avatar.framework; sourceTree = BUILT_PRODUCTS_DIR; };
503987951EF085630032142F /* Avatar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Avatar.h; sourceTree = "<group>"; };
503987961EF085630032142F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5039879D1EF0857D0032142F /* Avatar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Avatar.swift; path = ../../../Documents/xcode/Avatar/Avatar/Avatar.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
@@ -50,7 +52,8 @@
children = (
503987951EF085630032142F /* Avatar.h */,
503987961EF085630032142F /* Info.plist */,
5039879D1EF0857D0032142F /* Avatar.swift */,
50017B3B1F448F6D00058FF6 /* Avatar.swift */,
50017B3F1F44A20500058FF6 /* AvatarSeed.swift */,
);
path = Avatar;
sourceTree = "<group>";
@@ -62,7 +65,9 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
50017B3C1F448F6D00058FF6 /* Avatar.swift in Headers */,
503987971EF085630032142F /* Avatar.h in Headers */,
50017B401F44A20500058FF6 /* AvatarSeed.swift in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -134,7 +139,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5039879E1EF0857D0032142F /* Avatar.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Binary file not shown.
85 changes: 54 additions & 31 deletions Avatar/Avatar.swift
Original file line number Diff line number Diff line change
@@ -14,58 +14,64 @@ import UIKit
typealias Colors = (primary: UIColor, secondary: UIColor, tertiary: UIColor)

open class Avatar {

private class func getRandomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(256)) / 255.0
let green = CGFloat(arc4random_uniform(256)) / 255.0
let blue = CGFloat(arc4random_uniform(256)) / 255.0

return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

private class func colors() -> Colors {
let colors:Colors = (primary: getRandomColor(), secondary: getRandomColor(), tertiary: getRandomColor())
return colors
}

private class func getImageMap(length: Int) -> [Int] {
var map = [Int]()

for _ in 0..<length {
map.append(Int(arc4random_uniform(3)))
}

return map
}

open class func generate(for size: CGSize, scale: Int?) -> UIImage? {

let width = Int(size.width)
let height = Int(size.height)


public static var customSeed:AvatarSeed?

open class func generate(seed: AvatarSeed) -> UIImage? {
customSeed = seed
return generate(for: seed.size, scale: seed.scale)
}

open class func generate(for size: CGSize, scale: Int? , complete:@escaping (_ seed: AvatarSeed, _ image:UIImage?) -> ()) {
let width = Int(customSeed?.size.width ?? size.width)
let height = Int(customSeed?.size.height ?? size.height)

let pixelSize = scale ?? 20

let totalColumns = width / pixelSize
let totalRows = height / pixelSize

let wRemainder = width % pixelSize
let hRemainder = height % pixelSize

// if #available(iOS 11, *) {
// let wRemainder = width.dividedReportingOverflow(by: pixelSize).partialValue
// let hRemainder = height.dividedReportingOverflow(by: pixelSize).partialValue
// }

let mapValues = getImageMap(length: totalColumns * totalRows)

let colors = Avatar.colors()

let mapValues = customSeed?.map ?? getImageMap(length: totalColumns * totalRows)
let colors = customSeed?.colors ?? Avatar.colors()
var x = 0 //columns counter
var y = 0 //rows counter

UIGraphicsBeginImageContextWithOptions(size, true, 1)
let context = UIGraphicsGetCurrentContext()!

for position in 0..<mapValues.count {
//context stuff
let colorIndex = mapValues[position]
@@ -80,25 +86,42 @@ open class Avatar {
default:
color = .black
}

context.setFillColor(color.cgColor)
context.fill(CGRect(x: CGFloat(x * pixelSize), y:CGFloat(y * pixelSize), width:CGFloat(pixelSize + (pixelSize * wRemainder)), height:CGFloat(pixelSize + (pixelSize * hRemainder))));

x = x + 1

if x == totalColumns {
x = 0
y = y + 1
}


}

let outputImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext()


let seed = AvatarSeed(map: mapValues, colors: colors, size: size, scale:pixelSize)

complete(seed, outputImage)
}

open class func generate(for size: CGSize, scale: Int?) -> UIImage? {
var outputImage: UIImage?

self.generate(for: size, scale: scale) { (seed, image) in
outputImage = image
}

return outputImage
}

}





25 changes: 25 additions & 0 deletions Avatar/AvatarSeed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AvatarMap.swift
// Avatar
//
// Created by William Vabrinskas on 8/16/17.
// Copyright © 2017 williamvabrinskas. All rights reserved.
//

import Foundation
import UIKit

public struct AvatarSeed {
var map:[Int]!
var colors:Colors!
var size:CGSize!
var scale:Int!

init(map:[Int], colors:Colors!, size:CGSize, scale:Int) {
self.map = map
self.colors = colors
self.size = size
self.scale = scale
}

}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# CHANGELOG

# 1.0.2
* Added ability to generate and use a seed object, `AvatarSeed`, to create an `Avatar`.

0 comments on commit d56a62a

Please sign in to comment.