Skip to content

Instantly share code, notes, and snippets.

@alana-mullen
Created March 14, 2017 03:07
Show Gist options
  • Save alana-mullen/b0eba855c0307b47168be85291788746 to your computer and use it in GitHub Desktop.
Save alana-mullen/b0eba855c0307b47168be85291788746 to your computer and use it in GitHub Desktop.
Use current iOS app icon with UIImageView in Swift 3
//
// GetAppIcon.swift
//
// Get the AppIcon from Assets and return it as a UIImage
// Created by Stephen Mullen
//
import UIKit
func getAppIcon() -> UIImage {
let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? NSDictionary
let primaryIconsDictionary = iconsDictionary?["CFBundlePrimaryIcon"] as? NSDictionary
let iconFiles = primaryIconsDictionary!["CFBundleIconFiles"] as! NSArray
// First will be smallest for the device class, last will be the largest for device class
let lastIcon = iconFiles.lastObject as! NSString
let icon = UIImage(named: lastIcon as String)
return icon!
}
@ftp27
Copy link

ftp27 commented Sep 5, 2017

extension UIImage {
    static var appIcon: UIImage? {
        guard let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String:Any],
              let primaryIconsDictionary = iconsDictionary["CFBundlePrimaryIcon"] as? [String:Any],
              let iconFiles = primaryIconsDictionary["CFBundleIconFiles"] as? [String],
              let lastIcon = iconFiles.last else { return nil }
        return UIImage(named: lastIcon)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment