├── .gitignore ├── LICENSE ├── README.md ├── SwiftAsciiArt.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── SwiftAsciiArt ├── AppDelegate.swift ├── AsciiArtist.swift ├── AsciiPalette.swift ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── Pixel.swift ├── UIImage+Util.swift └── ViewController.swift └── images ├── batman.png ├── kermit.png └── monkey.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Josh Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Swift ASCII Art Generator 2 | 3 | Converts an image to text for no good reason. The code is pretty cool, though. It's pure functional Swifty goodness. 4 | 5 | Take a guided tour of the source code on my blog: 6 | 7 | http://ijoshsmith.com/2015/04/29/creating-ascii-art-in-functional-swift/ 8 | 9 | ![alt text](https://ijoshsmith.files.wordpress.com/2015/04/ascii_kermit.png "ASCII Kermit is thinking") 10 | -------------------------------------------------------------------------------- /SwiftAsciiArt.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 0384D6CC1AEC4BB900242145 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0384D6CB1AEC4BB900242145 /* AppDelegate.swift */; }; 11 | 0384D6CE1AEC4BB900242145 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0384D6CD1AEC4BB900242145 /* ViewController.swift */; }; 12 | 0384D6D11AEC4BB900242145 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0384D6CF1AEC4BB900242145 /* Main.storyboard */; }; 13 | 0384D6D31AEC4BB900242145 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0384D6D21AEC4BB900242145 /* Images.xcassets */; }; 14 | 0384D6D61AEC4BB900242145 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0384D6D41AEC4BB900242145 /* LaunchScreen.xib */; }; 15 | 0384D6EC1AEC4D6500242145 /* AsciiArtist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0384D6EB1AEC4D6500242145 /* AsciiArtist.swift */; }; 16 | 0384D6EE1AEC4D7700242145 /* kermit.png in Resources */ = {isa = PBXBuildFile; fileRef = 0384D6ED1AEC4D7700242145 /* kermit.png */; }; 17 | 0384D6F11AEC66C300242145 /* Pixel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0384D6F01AEC66C300242145 /* Pixel.swift */; }; 18 | 03F080901AED4F1600A20334 /* UIImage+Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F0808F1AED4F1600A20334 /* UIImage+Util.swift */; }; 19 | 03F080921AED5CF700A20334 /* monkey.png in Resources */ = {isa = PBXBuildFile; fileRef = 03F080911AED5CF700A20334 /* monkey.png */; }; 20 | 03F080961AEDD29200A20334 /* batman.png in Resources */ = {isa = PBXBuildFile; fileRef = 03F080951AEDD29200A20334 /* batman.png */; }; 21 | 03F080981AEDD7D000A20334 /* AsciiPalette.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F080971AEDD7D000A20334 /* AsciiPalette.swift */; }; 22 | /* End PBXBuildFile section */ 23 | 24 | /* Begin PBXFileReference section */ 25 | 0384D6C61AEC4BB900242145 /* SwiftAsciiArt.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftAsciiArt.app; sourceTree = BUILT_PRODUCTS_DIR; }; 26 | 0384D6CA1AEC4BB900242145 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | 0384D6CB1AEC4BB900242145 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 28 | 0384D6CD1AEC4BB900242145 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 29 | 0384D6D01AEC4BB900242145 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 30 | 0384D6D21AEC4BB900242145 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 31 | 0384D6D51AEC4BB900242145 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 32 | 0384D6EB1AEC4D6500242145 /* AsciiArtist.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AsciiArtist.swift; path = SwiftAsciiArt/AsciiArtist.swift; sourceTree = ""; }; 33 | 0384D6ED1AEC4D7700242145 /* kermit.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = kermit.png; path = images/kermit.png; sourceTree = ""; }; 34 | 0384D6F01AEC66C300242145 /* Pixel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Pixel.swift; path = SwiftAsciiArt/Pixel.swift; sourceTree = ""; }; 35 | 03F0808F1AED4F1600A20334 /* UIImage+Util.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIImage+Util.swift"; path = "SwiftAsciiArt/UIImage+Util.swift"; sourceTree = ""; }; 36 | 03F080911AED5CF700A20334 /* monkey.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = monkey.png; path = images/monkey.png; sourceTree = ""; }; 37 | 03F080951AEDD29200A20334 /* batman.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = batman.png; path = images/batman.png; sourceTree = ""; }; 38 | 03F080971AEDD7D000A20334 /* AsciiPalette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AsciiPalette.swift; path = SwiftAsciiArt/AsciiPalette.swift; sourceTree = ""; }; 39 | /* End PBXFileReference section */ 40 | 41 | /* Begin PBXFrameworksBuildPhase section */ 42 | 0384D6C31AEC4BB900242145 /* Frameworks */ = { 43 | isa = PBXFrameworksBuildPhase; 44 | buildActionMask = 2147483647; 45 | files = ( 46 | ); 47 | runOnlyForDeploymentPostprocessing = 0; 48 | }; 49 | /* End PBXFrameworksBuildPhase section */ 50 | 51 | /* Begin PBXGroup section */ 52 | 0384D6BD1AEC4BB900242145 = { 53 | isa = PBXGroup; 54 | children = ( 55 | 0384D6EB1AEC4D6500242145 /* AsciiArtist.swift */, 56 | 03F080971AEDD7D000A20334 /* AsciiPalette.swift */, 57 | 0384D6F01AEC66C300242145 /* Pixel.swift */, 58 | 03F0808F1AED4F1600A20334 /* UIImage+Util.swift */, 59 | 0384D6C81AEC4BB900242145 /* app */, 60 | 0384D6EF1AEC4D7B00242145 /* images */, 61 | 0384D6C71AEC4BB900242145 /* products */, 62 | ); 63 | sourceTree = ""; 64 | }; 65 | 0384D6C71AEC4BB900242145 /* products */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 0384D6C61AEC4BB900242145 /* SwiftAsciiArt.app */, 69 | ); 70 | name = products; 71 | sourceTree = ""; 72 | }; 73 | 0384D6C81AEC4BB900242145 /* app */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 0384D6CD1AEC4BB900242145 /* ViewController.swift */, 77 | 0384D6CF1AEC4BB900242145 /* Main.storyboard */, 78 | 0384D6C91AEC4BB900242145 /* Supporting Files */, 79 | ); 80 | name = app; 81 | path = SwiftAsciiArt; 82 | sourceTree = ""; 83 | }; 84 | 0384D6C91AEC4BB900242145 /* Supporting Files */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 0384D6CB1AEC4BB900242145 /* AppDelegate.swift */, 88 | 0384D6D21AEC4BB900242145 /* Images.xcassets */, 89 | 0384D6CA1AEC4BB900242145 /* Info.plist */, 90 | 0384D6D41AEC4BB900242145 /* LaunchScreen.xib */, 91 | ); 92 | name = "Supporting Files"; 93 | sourceTree = ""; 94 | }; 95 | 0384D6EF1AEC4D7B00242145 /* images */ = { 96 | isa = PBXGroup; 97 | children = ( 98 | 03F080951AEDD29200A20334 /* batman.png */, 99 | 0384D6ED1AEC4D7700242145 /* kermit.png */, 100 | 03F080911AED5CF700A20334 /* monkey.png */, 101 | ); 102 | name = images; 103 | sourceTree = ""; 104 | }; 105 | /* End PBXGroup section */ 106 | 107 | /* Begin PBXNativeTarget section */ 108 | 0384D6C51AEC4BB900242145 /* SwiftAsciiArt */ = { 109 | isa = PBXNativeTarget; 110 | buildConfigurationList = 0384D6E51AEC4BBA00242145 /* Build configuration list for PBXNativeTarget "SwiftAsciiArt" */; 111 | buildPhases = ( 112 | 0384D6C21AEC4BB900242145 /* Sources */, 113 | 0384D6C31AEC4BB900242145 /* Frameworks */, 114 | 0384D6C41AEC4BB900242145 /* Resources */, 115 | ); 116 | buildRules = ( 117 | ); 118 | dependencies = ( 119 | ); 120 | name = SwiftAsciiArt; 121 | productName = SwiftAsciiArt; 122 | productReference = 0384D6C61AEC4BB900242145 /* SwiftAsciiArt.app */; 123 | productType = "com.apple.product-type.application"; 124 | }; 125 | /* End PBXNativeTarget section */ 126 | 127 | /* Begin PBXProject section */ 128 | 0384D6BE1AEC4BB900242145 /* Project object */ = { 129 | isa = PBXProject; 130 | attributes = { 131 | LastSwiftMigration = 0710; 132 | LastSwiftUpdateCheck = 0710; 133 | LastUpgradeCheck = 1000; 134 | ORGANIZATIONNAME = iJoshSmith; 135 | TargetAttributes = { 136 | 0384D6C51AEC4BB900242145 = { 137 | CreatedOnToolsVersion = 6.3.1; 138 | LastSwiftMigration = 1000; 139 | }; 140 | }; 141 | }; 142 | buildConfigurationList = 0384D6C11AEC4BB900242145 /* Build configuration list for PBXProject "SwiftAsciiArt" */; 143 | compatibilityVersion = "Xcode 3.2"; 144 | developmentRegion = English; 145 | hasScannedForEncodings = 0; 146 | knownRegions = ( 147 | en, 148 | Base, 149 | ); 150 | mainGroup = 0384D6BD1AEC4BB900242145; 151 | productRefGroup = 0384D6C71AEC4BB900242145 /* products */; 152 | projectDirPath = ""; 153 | projectRoot = ""; 154 | targets = ( 155 | 0384D6C51AEC4BB900242145 /* SwiftAsciiArt */, 156 | ); 157 | }; 158 | /* End PBXProject section */ 159 | 160 | /* Begin PBXResourcesBuildPhase section */ 161 | 0384D6C41AEC4BB900242145 /* Resources */ = { 162 | isa = PBXResourcesBuildPhase; 163 | buildActionMask = 2147483647; 164 | files = ( 165 | 03F080921AED5CF700A20334 /* monkey.png in Resources */, 166 | 0384D6D11AEC4BB900242145 /* Main.storyboard in Resources */, 167 | 03F080961AEDD29200A20334 /* batman.png in Resources */, 168 | 0384D6EE1AEC4D7700242145 /* kermit.png in Resources */, 169 | 0384D6D61AEC4BB900242145 /* LaunchScreen.xib in Resources */, 170 | 0384D6D31AEC4BB900242145 /* Images.xcassets in Resources */, 171 | ); 172 | runOnlyForDeploymentPostprocessing = 0; 173 | }; 174 | /* End PBXResourcesBuildPhase section */ 175 | 176 | /* Begin PBXSourcesBuildPhase section */ 177 | 0384D6C21AEC4BB900242145 /* Sources */ = { 178 | isa = PBXSourcesBuildPhase; 179 | buildActionMask = 2147483647; 180 | files = ( 181 | 0384D6F11AEC66C300242145 /* Pixel.swift in Sources */, 182 | 03F080981AEDD7D000A20334 /* AsciiPalette.swift in Sources */, 183 | 0384D6EC1AEC4D6500242145 /* AsciiArtist.swift in Sources */, 184 | 0384D6CE1AEC4BB900242145 /* ViewController.swift in Sources */, 185 | 03F080901AED4F1600A20334 /* UIImage+Util.swift in Sources */, 186 | 0384D6CC1AEC4BB900242145 /* AppDelegate.swift in Sources */, 187 | ); 188 | runOnlyForDeploymentPostprocessing = 0; 189 | }; 190 | /* End PBXSourcesBuildPhase section */ 191 | 192 | /* Begin PBXVariantGroup section */ 193 | 0384D6CF1AEC4BB900242145 /* Main.storyboard */ = { 194 | isa = PBXVariantGroup; 195 | children = ( 196 | 0384D6D01AEC4BB900242145 /* Base */, 197 | ); 198 | name = Main.storyboard; 199 | sourceTree = ""; 200 | }; 201 | 0384D6D41AEC4BB900242145 /* LaunchScreen.xib */ = { 202 | isa = PBXVariantGroup; 203 | children = ( 204 | 0384D6D51AEC4BB900242145 /* Base */, 205 | ); 206 | name = LaunchScreen.xib; 207 | sourceTree = ""; 208 | }; 209 | /* End PBXVariantGroup section */ 210 | 211 | /* Begin XCBuildConfiguration section */ 212 | 0384D6E31AEC4BBA00242145 /* Debug */ = { 213 | isa = XCBuildConfiguration; 214 | buildSettings = { 215 | ALWAYS_SEARCH_USER_PATHS = NO; 216 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 217 | CLANG_CXX_LIBRARY = "libc++"; 218 | CLANG_ENABLE_MODULES = YES; 219 | CLANG_ENABLE_OBJC_ARC = YES; 220 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 221 | CLANG_WARN_BOOL_CONVERSION = YES; 222 | CLANG_WARN_COMMA = YES; 223 | CLANG_WARN_CONSTANT_CONVERSION = YES; 224 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 225 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 226 | CLANG_WARN_EMPTY_BODY = YES; 227 | CLANG_WARN_ENUM_CONVERSION = YES; 228 | CLANG_WARN_INFINITE_RECURSION = YES; 229 | CLANG_WARN_INT_CONVERSION = YES; 230 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 231 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 232 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 233 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 234 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 235 | CLANG_WARN_STRICT_PROTOTYPES = YES; 236 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 237 | CLANG_WARN_UNREACHABLE_CODE = YES; 238 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 239 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 240 | COPY_PHASE_STRIP = NO; 241 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 242 | ENABLE_STRICT_OBJC_MSGSEND = YES; 243 | ENABLE_TESTABILITY = YES; 244 | GCC_C_LANGUAGE_STANDARD = gnu99; 245 | GCC_DYNAMIC_NO_PIC = NO; 246 | GCC_NO_COMMON_BLOCKS = YES; 247 | GCC_OPTIMIZATION_LEVEL = 0; 248 | GCC_PREPROCESSOR_DEFINITIONS = ( 249 | "DEBUG=1", 250 | "$(inherited)", 251 | ); 252 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 253 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 254 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 255 | GCC_WARN_UNDECLARED_SELECTOR = YES; 256 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 257 | GCC_WARN_UNUSED_FUNCTION = YES; 258 | GCC_WARN_UNUSED_VARIABLE = YES; 259 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 260 | MTL_ENABLE_DEBUG_INFO = YES; 261 | ONLY_ACTIVE_ARCH = YES; 262 | SDKROOT = iphoneos; 263 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 264 | TARGETED_DEVICE_FAMILY = "1,2"; 265 | }; 266 | name = Debug; 267 | }; 268 | 0384D6E41AEC4BBA00242145 /* Release */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ALWAYS_SEARCH_USER_PATHS = NO; 272 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 273 | CLANG_CXX_LIBRARY = "libc++"; 274 | CLANG_ENABLE_MODULES = YES; 275 | CLANG_ENABLE_OBJC_ARC = YES; 276 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 277 | CLANG_WARN_BOOL_CONVERSION = YES; 278 | CLANG_WARN_COMMA = YES; 279 | CLANG_WARN_CONSTANT_CONVERSION = YES; 280 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 281 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 282 | CLANG_WARN_EMPTY_BODY = YES; 283 | CLANG_WARN_ENUM_CONVERSION = YES; 284 | CLANG_WARN_INFINITE_RECURSION = YES; 285 | CLANG_WARN_INT_CONVERSION = YES; 286 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 287 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 288 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 289 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 290 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 291 | CLANG_WARN_STRICT_PROTOTYPES = YES; 292 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 293 | CLANG_WARN_UNREACHABLE_CODE = YES; 294 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 295 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 296 | COPY_PHASE_STRIP = NO; 297 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 298 | ENABLE_NS_ASSERTIONS = NO; 299 | ENABLE_STRICT_OBJC_MSGSEND = YES; 300 | GCC_C_LANGUAGE_STANDARD = gnu99; 301 | GCC_NO_COMMON_BLOCKS = YES; 302 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 303 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 304 | GCC_WARN_UNDECLARED_SELECTOR = YES; 305 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 306 | GCC_WARN_UNUSED_FUNCTION = YES; 307 | GCC_WARN_UNUSED_VARIABLE = YES; 308 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 309 | MTL_ENABLE_DEBUG_INFO = NO; 310 | SDKROOT = iphoneos; 311 | SWIFT_COMPILATION_MODE = wholemodule; 312 | TARGETED_DEVICE_FAMILY = "1,2"; 313 | VALIDATE_PRODUCT = YES; 314 | }; 315 | name = Release; 316 | }; 317 | 0384D6E61AEC4BBA00242145 /* Debug */ = { 318 | isa = XCBuildConfiguration; 319 | buildSettings = { 320 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 321 | INFOPLIST_FILE = SwiftAsciiArt/Info.plist; 322 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 323 | PRODUCT_BUNDLE_IDENTIFIER = "iJoshSmith.$(PRODUCT_NAME:rfc1034identifier)"; 324 | PRODUCT_NAME = "$(TARGET_NAME)"; 325 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 326 | SWIFT_VERSION = 4.2; 327 | }; 328 | name = Debug; 329 | }; 330 | 0384D6E71AEC4BBA00242145 /* Release */ = { 331 | isa = XCBuildConfiguration; 332 | buildSettings = { 333 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 334 | INFOPLIST_FILE = SwiftAsciiArt/Info.plist; 335 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 336 | PRODUCT_BUNDLE_IDENTIFIER = "iJoshSmith.$(PRODUCT_NAME:rfc1034identifier)"; 337 | PRODUCT_NAME = "$(TARGET_NAME)"; 338 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 339 | SWIFT_VERSION = 4.2; 340 | }; 341 | name = Release; 342 | }; 343 | /* End XCBuildConfiguration section */ 344 | 345 | /* Begin XCConfigurationList section */ 346 | 0384D6C11AEC4BB900242145 /* Build configuration list for PBXProject "SwiftAsciiArt" */ = { 347 | isa = XCConfigurationList; 348 | buildConfigurations = ( 349 | 0384D6E31AEC4BBA00242145 /* Debug */, 350 | 0384D6E41AEC4BBA00242145 /* Release */, 351 | ); 352 | defaultConfigurationIsVisible = 0; 353 | defaultConfigurationName = Release; 354 | }; 355 | 0384D6E51AEC4BBA00242145 /* Build configuration list for PBXNativeTarget "SwiftAsciiArt" */ = { 356 | isa = XCConfigurationList; 357 | buildConfigurations = ( 358 | 0384D6E61AEC4BBA00242145 /* Debug */, 359 | 0384D6E71AEC4BBA00242145 /* Release */, 360 | ); 361 | defaultConfigurationIsVisible = 0; 362 | defaultConfigurationName = Release; 363 | }; 364 | /* End XCConfigurationList section */ 365 | }; 366 | rootObject = 0384D6BE1AEC4BB900242145 /* Project object */; 367 | } 368 | -------------------------------------------------------------------------------- /SwiftAsciiArt.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftAsciiArt.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftAsciiArt/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/25/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate 13 | { 14 | var window: UIWindow? 15 | } 16 | -------------------------------------------------------------------------------- /SwiftAsciiArt/AsciiArtist.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AsciiArtist.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/25/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | /** Transforms an image to ASCII art. */ 13 | class AsciiArtist 14 | { 15 | fileprivate let 16 | image: UIImage, 17 | palette: AsciiPalette 18 | 19 | init(_ image: UIImage, _ palette: AsciiPalette) 20 | { 21 | self.image = image 22 | self.palette = palette 23 | } 24 | 25 | func createAsciiArt() -> String 26 | { 27 | let 28 | dataProvider = image.cgImage?.dataProvider, 29 | pixelData = dataProvider?.data, 30 | pixelPointer = CFDataGetBytePtr(pixelData), 31 | intensities = intensityMatrixFromPixelPointer(pixelPointer!), 32 | symbolMatrix = symbolMatrixFromIntensityMatrix(intensities) 33 | return symbolMatrix.joined(separator: "\n") 34 | } 35 | 36 | fileprivate func intensityMatrixFromPixelPointer(_ pointer: PixelPointer) -> [[Double]] 37 | { 38 | let 39 | width = Int(image.size.width), 40 | height = Int(image.size.height), 41 | matrix = Pixel.createPixelMatrix(width, height) 42 | return matrix.map { pixelRow in 43 | pixelRow.map { pixel in 44 | pixel.intensityFromPixelPointer(pointer) 45 | } 46 | } 47 | } 48 | 49 | fileprivate func symbolMatrixFromIntensityMatrix(_ matrix: [[Double]]) -> [String] 50 | { 51 | return matrix.map { intensityRow in 52 | intensityRow.reduce("") { 53 | $0 + self.symbolFromIntensity($1) 54 | } 55 | } 56 | } 57 | 58 | fileprivate func symbolFromIntensity(_ intensity: Double) -> String 59 | { 60 | assert(0.0 <= intensity && intensity <= 1.0) 61 | 62 | let 63 | factor = palette.symbols.count - 1, 64 | value = round(intensity * Double(factor)), 65 | index = Int(value) 66 | return palette.symbols[index] 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /SwiftAsciiArt/AsciiPalette.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AsciiPalette.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/26/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | /** Provides a list of ASCII symbols sorted from darkest to brightest. */ 13 | class AsciiPalette 14 | { 15 | fileprivate let font: UIFont 16 | 17 | init(font: UIFont) { self.font = font } 18 | 19 | lazy var symbols: [String] = self.loadSymbols() 20 | 21 | fileprivate func loadSymbols() -> [String] 22 | { 23 | return symbolsSortedByIntensityForAsciiCodes(32...126) // from ' ' to '~' 24 | } 25 | 26 | fileprivate func symbolsSortedByIntensityForAsciiCodes(_ codes: CountableClosedRange) -> [String] 27 | { 28 | let 29 | symbols = codes.map { self.symbolFromAsciiCode($0) }, 30 | symbolImages = symbols.map { UIImage.imageOfSymbol($0, self.font) }, 31 | whitePixelCounts = symbolImages.map { self.countWhitePixelsInImage($0) }, 32 | sortedSymbols = sortByIntensity(symbols, whitePixelCounts) 33 | return sortedSymbols 34 | } 35 | 36 | fileprivate func symbolFromAsciiCode(_ code: Int) -> String 37 | { 38 | return String(Character(UnicodeScalar(code)!)) 39 | } 40 | 41 | fileprivate func countWhitePixelsInImage(_ image: UIImage) -> Int 42 | { 43 | let 44 | dataProvider = image.cgImage?.dataProvider, 45 | pixelData = dataProvider?.data, 46 | pixelPointer = CFDataGetBytePtr(pixelData), 47 | byteCount = CFDataGetLength(pixelData), 48 | pixelOffsets = stride(from: 0, to: byteCount, by: Pixel.bytesPerPixel) 49 | return pixelOffsets.reduce(0) { (count, offset) -> Int in 50 | let 51 | r = pixelPointer?[offset + 0], 52 | g = pixelPointer?[offset + 1], 53 | b = pixelPointer?[offset + 2], 54 | isWhite = (r == 255) && (g == 255) && (b == 255) 55 | return isWhite ? count + 1 : count 56 | } 57 | } 58 | 59 | fileprivate func sortByIntensity(_ symbols: [String], _ whitePixelCounts: [Int]) -> [String] 60 | { 61 | let 62 | mappings = NSDictionary(objects: symbols, forKeys: whitePixelCounts as [NSCopying]), 63 | uniqueCounts = Set(whitePixelCounts), 64 | sortedCounts = uniqueCounts.sorted(), 65 | sortedSymbols = sortedCounts.map { mappings[$0] as! String } 66 | return sortedSymbols 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /SwiftAsciiArt/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /SwiftAsciiArt/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 31 | 41 | 51 | 61 | 62 | 63 | 64 | 65 | 66 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /SwiftAsciiArt/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /SwiftAsciiArt/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ASCII Art 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | NSPhotoLibraryUsageDescription 26 | Load image and display ASCII Art for it 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /SwiftAsciiArt/Pixel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Pixel.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/25/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | /** Represents the memory address of a pixel. */ 12 | typealias PixelPointer = UnsafePointer 13 | 14 | /** A point in an image converted to an ASCII character. */ 15 | struct Pixel 16 | { 17 | /** The number of bytes a pixel occupies. 1 byte per channel (RGBA). */ 18 | static let bytesPerPixel = 4 19 | 20 | fileprivate let offset: Int 21 | fileprivate init(_ offset: Int) { self.offset = offset } 22 | 23 | static func createPixelMatrix(_ width: Int, _ height: Int) -> [[Pixel]] 24 | { 25 | return (0.. Double 34 | { 35 | let 36 | red = pointer[offset + 0], 37 | green = pointer[offset + 1], 38 | blue = pointer[offset + 2] 39 | return Pixel.calculateIntensity(red, green, blue) 40 | } 41 | 42 | fileprivate static func calculateIntensity(_ r: UInt8, _ g: UInt8, _ b: UInt8) -> Double 43 | { 44 | // Normalize the pixel's grayscale value to between 0 and 1. 45 | // Weights from http://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems 46 | let 47 | redWeight = 0.229, 48 | greenWeight = 0.587, 49 | blueWeight = 0.114, 50 | weightedMax = 255.0 * redWeight + 51 | 255.0 * greenWeight + 52 | 255.0 * blueWeight, 53 | weightedSum = Double(r) * redWeight + 54 | Double(g) * greenWeight + 55 | Double(b) * blueWeight 56 | return weightedSum / weightedMax 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /SwiftAsciiArt/UIImage+Util.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+Util.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/26/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import AVFoundation 10 | import Foundation 11 | import UIKit 12 | 13 | extension UIImage 14 | { 15 | class func imageOfSymbol(_ symbol: String, _ font: UIFont) -> UIImage 16 | { 17 | let 18 | length = font.pointSize * 2, 19 | size = CGSize(width: length, height: length), 20 | rect = CGRect(origin: CGPoint.zero, size: size) 21 | 22 | UIGraphicsBeginImageContext(size) 23 | let context = UIGraphicsGetCurrentContext() 24 | 25 | // Fill the background with white. 26 | context?.setFillColor(UIColor.white.cgColor) 27 | context?.fill(rect) 28 | 29 | // Draw the character with black. 30 | let nsString = NSString(string: symbol) 31 | nsString.draw(at: rect.origin, withAttributes: convertToOptionalNSAttributedStringKeyDictionary([ 32 | convertFromNSAttributedStringKey(NSAttributedString.Key.font): font, 33 | convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor): UIColor.black 34 | ])) 35 | 36 | let image = UIGraphicsGetImageFromCurrentImageContext()! 37 | UIGraphicsEndImageContext() 38 | return image 39 | } 40 | 41 | func imageConstrainedToMaxSize(_ maxSize: CGSize) -> UIImage 42 | { 43 | let isTooBig = 44 | size.width > maxSize.width || 45 | size.height > maxSize.height 46 | if isTooBig 47 | { 48 | let 49 | maxRect = CGRect(origin: CGPoint.zero, size: maxSize), 50 | scaledRect = AVMakeRect(aspectRatio: self.size, insideRect: maxRect), 51 | scaledSize = scaledRect.size, 52 | targetRect = CGRect(origin: CGPoint.zero, size: scaledSize), 53 | width = Int(scaledSize.width), 54 | height = Int(scaledSize.height), 55 | cgImage = self.cgImage, 56 | bitsPerComp = cgImage?.bitsPerComponent, 57 | compsPerPixel = 4, // RGBA 58 | bytesPerRow = width * compsPerPixel, 59 | colorSpace = cgImage?.colorSpace, 60 | bitmapInfo = cgImage?.bitmapInfo, 61 | context = CGContext( 62 | data: nil, 63 | width: width, 64 | height: height, 65 | bitsPerComponent: bitsPerComp!, 66 | bytesPerRow: bytesPerRow, 67 | space: colorSpace!, 68 | bitmapInfo: (bitmapInfo?.rawValue)!) 69 | 70 | if context != nil 71 | { 72 | context!.interpolationQuality = CGInterpolationQuality.low 73 | context?.draw(cgImage!, in: targetRect) 74 | if let scaledCGImage = context?.makeImage() 75 | { 76 | return UIImage(cgImage: scaledCGImage) 77 | } 78 | } 79 | } 80 | return self 81 | } 82 | 83 | func imageRotatedToPortraitOrientation() -> UIImage 84 | { 85 | let mustRotate = self.imageOrientation != .up 86 | if mustRotate 87 | { 88 | let rotatedSize = CGSize(width: size.height, height: size.width) 89 | UIGraphicsBeginImageContext(rotatedSize) 90 | if let context = UIGraphicsGetCurrentContext() 91 | { 92 | // Perform the rotation and scale transforms around the image's center. 93 | context.translateBy(x: rotatedSize.width/2, y: rotatedSize.height/2) 94 | 95 | // Rotate the image upright. 96 | let 97 | degrees = self.degreesToRotate(), 98 | radians = degrees * M_PI / 180.0 99 | context.rotate(by: CGFloat(radians)) 100 | 101 | // Flip the image on the Y axis. 102 | context.scaleBy(x: 1.0, y: -1.0) 103 | 104 | let 105 | targetOrigin = CGPoint(x: -size.width/2, y: -size.height/2), 106 | targetRect = CGRect(origin: targetOrigin, size: self.size) 107 | 108 | context.draw(self.cgImage!, in: targetRect) 109 | let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()! 110 | UIGraphicsEndImageContext() 111 | 112 | return rotatedImage 113 | } 114 | } 115 | return self 116 | } 117 | 118 | fileprivate func degreesToRotate() -> Double 119 | { 120 | switch self.imageOrientation 121 | { 122 | case .right: return 90 123 | case .down: return 180 124 | case .left: return -90 125 | default: return 0 126 | } 127 | } 128 | } 129 | 130 | // Helper function inserted by Swift 4.2 migrator. 131 | fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? { 132 | guard let input = input else { return nil } 133 | return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)}) 134 | } 135 | 136 | // Helper function inserted by Swift 4.2 migrator. 137 | fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String { 138 | return input.rawValue 139 | } 140 | -------------------------------------------------------------------------------- /SwiftAsciiArt/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // SwiftAsciiArt 4 | // 5 | // Created by Joshua Smith on 4/25/15. 6 | // Copyright (c) 2015 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: 12 | UIViewController, 13 | UIImagePickerControllerDelegate, 14 | UINavigationControllerDelegate, // required by image picker 15 | UIScrollViewDelegate 16 | { 17 | fileprivate let labelFont = UIFont(name: "Menlo", size: 7)! 18 | fileprivate let maxImageSize = CGSize(width: 310, height: 310) 19 | fileprivate lazy var palette: AsciiPalette = AsciiPalette(font: self.labelFont) 20 | 21 | fileprivate var currentLabel: UILabel? 22 | @IBOutlet weak var busyView: UIView! 23 | @IBOutlet weak var scrollView: UIScrollView! 24 | 25 | // MARK: - Setup 26 | 27 | override func viewDidLoad() 28 | { 29 | super.viewDidLoad() 30 | self.configureZoomSupport() 31 | } 32 | 33 | // MARK: - Actions 34 | 35 | @IBAction func handleKermitTapped(_ sender: AnyObject) 36 | { 37 | displayImageNamed("kermit") 38 | } 39 | 40 | @IBAction func handleBatmanTapped(_ sender: AnyObject) 41 | { 42 | displayImageNamed("batman") 43 | } 44 | 45 | @IBAction func handleMonkeyTapped(_ sender: AnyObject) 46 | { 47 | displayImageNamed("monkey") 48 | } 49 | 50 | @IBAction func handlePickImageTapped(_ sender: AnyObject) 51 | { 52 | let imagePicker = UIImagePickerController() 53 | imagePicker.delegate = self 54 | self.show(imagePicker, sender: self) 55 | } 56 | 57 | // MARK: - UIImagePickerControllerDelegate 58 | 59 | func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 60 | { 61 | // Local variable inserted by Swift 4.2 migrator. 62 | let info = convertFromUIImagePickerControllerInfoKeyDictionary(info) 63 | 64 | self.dismiss(animated: true, completion: nil) 65 | 66 | if let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage 67 | { 68 | displayImage(image) 69 | } 70 | } 71 | 72 | func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 73 | { 74 | self.dismiss(animated: true, completion: nil) 75 | } 76 | 77 | // MARK: - Rendering 78 | 79 | fileprivate func displayImageNamed(_ imageName: String) 80 | { 81 | displayImage(UIImage(named: imageName)!) 82 | } 83 | 84 | fileprivate func displayImage(_ image: UIImage) 85 | { 86 | self.busyView.isHidden = false 87 | DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { 88 | 89 | let // Rotate first because the orientation is lost when resizing. 90 | rotatedImage = image.imageRotatedToPortraitOrientation(), 91 | resizedImage = rotatedImage.imageConstrainedToMaxSize(self.maxImageSize), 92 | asciiArtist = AsciiArtist(resizedImage, self.palette), 93 | asciiArt = asciiArtist.createAsciiArt() 94 | 95 | DispatchQueue.main.async { 96 | self.displayAsciiArt(asciiArt) 97 | self.busyView.isHidden = true 98 | } 99 | 100 | print(asciiArt) 101 | } 102 | } 103 | 104 | fileprivate func displayAsciiArt(_ asciiArt: String) 105 | { 106 | let label = UILabel() 107 | label.font = self.labelFont 108 | label.lineBreakMode = NSLineBreakMode.byClipping 109 | label.numberOfLines = 0 110 | label.text = asciiArt 111 | label.sizeToFit() 112 | 113 | currentLabel?.removeFromSuperview() 114 | currentLabel = label 115 | 116 | scrollView.addSubview(label) 117 | scrollView.contentSize = label.frame.size 118 | 119 | self.updateZoomSettings(animated: false) 120 | scrollView.contentOffset = CGPoint.zero 121 | } 122 | 123 | // MARK: - Zooming support 124 | 125 | fileprivate func configureZoomSupport() 126 | { 127 | scrollView.delegate = self 128 | scrollView.maximumZoomScale = 5 129 | } 130 | 131 | fileprivate func updateZoomSettings(animated: Bool) 132 | { 133 | let 134 | scrollSize = scrollView.frame.size, 135 | contentSize = scrollView.contentSize, 136 | scaleWidth = scrollSize.width / contentSize.width, 137 | scaleHeight = scrollSize.height / contentSize.height, 138 | scale = max(scaleWidth, scaleHeight) 139 | scrollView.minimumZoomScale = scale 140 | scrollView.setZoomScale(scale, animated: animated) 141 | } 142 | 143 | // MARK: - UIScrollViewDelegate 144 | 145 | func viewForZooming(in scrollView: UIScrollView) -> UIView? 146 | { 147 | return currentLabel 148 | } 149 | } 150 | 151 | // Helper function inserted by Swift 4.2 migrator. 152 | fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] { 153 | return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)}) 154 | } 155 | 156 | // Helper function inserted by Swift 4.2 migrator. 157 | fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String { 158 | return input.rawValue 159 | } 160 | -------------------------------------------------------------------------------- /images/batman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijoshsmith/swift-ascii-art/9079a07c679ca2fa6110ae0b0082f48724e471da/images/batman.png -------------------------------------------------------------------------------- /images/kermit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijoshsmith/swift-ascii-art/9079a07c679ca2fa6110ae0b0082f48724e471da/images/kermit.png -------------------------------------------------------------------------------- /images/monkey.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijoshsmith/swift-ascii-art/9079a07c679ca2fa6110ae0b0082f48724e471da/images/monkey.png --------------------------------------------------------------------------------