NSImage (named:String) Sample Code

NSImage (named:String) Sample Code

Created By:Debasis Das (11-Feb-2016)

In this post we will use all the image resources available in NSImage class.

These images are present in the AppKit Framework resources and are readily available for use in any Cocoa application using AppKit framework

for eg:

Image constant name = NSImageNameQuickLookTemplate (defined as let in the NSImage file)

Actual resource name = NSQuickLookTemplate

In the below sample we have used a view based NSTableView to display the image and the constants used for rendering each image

NSImage named 1NSImage named 2

NSImage named 3NSImage named 4

Sample code for the above table view representation

//  AppDelegate.swift
//  NSImageNamed-AllSamples
//  Created by Debasis Das on 11/02/16.
//  Copyright © 2016 Knowstack. All rights reserved.

import Cocoa

class KSImageTableCellView: NSView {
    
    @IBOutlet weak var imageView:NSImageView!
    @IBOutlet weak var imageName:NSTextField!
    @IBOutlet weak var imageNameConstant:NSTextField!
    
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {
    
    let imageArray:NSArray = [
        ["imageName":"NSImageNameQuickLookTemplate","theImage":NSImageNameQuickLookTemplate],
        ["imageName":"NSImageNameBluetoothTemplate","theImage": NSImageNameBluetoothTemplate],
        ["imageName":"NSImageNameIChatTheaterTemplate","theImage": NSImageNameIChatTheaterTemplate],
        ["imageName":"NSImageNameSlideshowTemplate","theImage": NSImageNameSlideshowTemplate],
        ["imageName":"NSImageNameActionTemplate","theImage": NSImageNameActionTemplate],
        ["imageName":"NSImageNameSmartBadgeTemplate","theImage": NSImageNameSmartBadgeTemplate],
        ["imageName":"NSImageNameIconViewTemplate","theImage": NSImageNameIconViewTemplate],
        ["imageName":"NSImageNameListViewTemplate","theImage": NSImageNameListViewTemplate],
        ["imageName":"NSImageNameColumnViewTemplate","theImage": NSImageNameColumnViewTemplate],
        ["imageName":"NSImageNameFlowViewTemplate","theImage": NSImageNameFlowViewTemplate],
        ["imageName":"NSImageNamePathTemplate","theImage": NSImageNamePathTemplate],
        ["imageName":"NSImageNameInvalidDataFreestandingTemplate","theImage": NSImageNameInvalidDataFreestandingTemplate],
        ["imageName":"NSImageNameLockLockedTemplate","theImage": NSImageNameLockLockedTemplate],
        ["imageName":"NSImageNameLockUnlockedTemplate","theImage": NSImageNameLockUnlockedTemplate],
        ["imageName":"NSImageNameGoRightTemplate","theImage": NSImageNameGoRightTemplate],
        ["imageName":"NSImageNameGoLeftTemplate","theImage": NSImageNameGoLeftTemplate],
        ["imageName":"NSImageNameRightFacingTriangleTemplate","theImage": NSImageNameRightFacingTriangleTemplate],
        ["imageName":"NSImageNameLeftFacingTriangleTemplate","theImage": NSImageNameLeftFacingTriangleTemplate],
        ["imageName":"NSImageNameAddTemplate","theImage": NSImageNameAddTemplate],
        ["imageName":"NSImageNameRemoveTemplate","theImage": NSImageNameRemoveTemplate],
        ["imageName":"NSImageNameRevealFreestandingTemplate","theImage": NSImageNameRevealFreestandingTemplate],
        ["imageName":"NSImageNameFollowLinkFreestandingTemplate","theImage": NSImageNameFollowLinkFreestandingTemplate],
        ["imageName":"NSImageNameEnterFullScreenTemplate","theImage": NSImageNameEnterFullScreenTemplate],
        ["imageName":"NSImageNameExitFullScreenTemplate","theImage": NSImageNameExitFullScreenTemplate],
        ["imageName":"NSImageNameStopProgressTemplate","theImage": NSImageNameStopProgressTemplate],
        ["imageName":"NSImageNameStopProgressFreestandingTemplate","theImage": NSImageNameStopProgressFreestandingTemplate],
        ["imageName":"NSImageNameRefreshTemplate","theImage": NSImageNameRefreshTemplate],
        ["imageName":"NSImageNameRefreshFreestandingTemplate","theImage": NSImageNameRefreshFreestandingTemplate],
        ["imageName":"NSImageNameBonjour","theImage": NSImageNameBonjour],
        ["imageName":"NSImageNameComputer","theImage": NSImageNameComputer],
        ["imageName":"NSImageNameFolderBurnable","theImage": NSImageNameFolderBurnable],
        ["imageName":"NSImageNameFolderSmart","theImage": NSImageNameFolderSmart],
        ["imageName":"NSImageNameFolder","theImage": NSImageNameFolder],
        ["imageName":"NSImageNameNetwork","theImage": NSImageNameNetwork],
        ["imageName":"NSImageNameMobileMe","theImage": NSImageNameMobileMe],
        ["imageName":"NSImageNameMultipleDocuments","theImage": NSImageNameMultipleDocuments],
        ["imageName":"NSImageNameUserAccounts","theImage": NSImageNameUserAccounts],
        ["imageName":"NSImageNamePreferencesGeneral","theImage": NSImageNamePreferencesGeneral],
        ["imageName":"NSImageNameAdvanced","theImage": NSImageNameAdvanced],
        ["imageName":"NSImageNameInfo","theImage": NSImageNameInfo],
        ["imageName":"NSImageNameFontPanel","theImage": NSImageNameFontPanel],
        ["imageName":"NSImageNameColorPanel","theImage": NSImageNameColorPanel],
        ["imageName":"NSImageNameUser","theImage": NSImageNameUser],
        ["imageName":"NSImageNameUserGroup","theImage": NSImageNameUserGroup],
        ["imageName":"NSImageNameEveryone","theImage": NSImageNameEveryone],
        ["imageName":"NSImageNameUserGuest","theImage": NSImageNameUserGuest],
        ["imageName":"NSImageNameMenuOnStateTemplate","theImage": NSImageNameMenuOnStateTemplate],
        ["imageName":"NSImageNameMenuMixedStateTemplate","theImage": NSImageNameMenuMixedStateTemplate],
        ["imageName":"NSImageNameApplicationIcon","theImage": NSImageNameApplicationIcon],
        ["imageName":"NSImageNameTrashEmpty","theImage": NSImageNameTrashEmpty],
        ["imageName":"NSImageNameTrashFull","theImage": NSImageNameTrashFull],
        ["imageName":"NSImageNameHomeTemplate","theImage": NSImageNameHomeTemplate],
        ["imageName":"NSImageNameBookmarksTemplate","theImage": NSImageNameBookmarksTemplate],
        ["imageName":"NSImageNameCaution","theImage": NSImageNameCaution],
        ["imageName":"NSImageNameStatusAvailable","theImage": NSImageNameStatusAvailable],
        ["imageName":"NSImageNameStatusPartiallyAvailable","theImage": NSImageNameStatusPartiallyAvailable],
        ["imageName":"NSImageNameStatusUnavailable","theImage": NSImageNameStatusUnavailable],
        ["imageName":"NSImageNameStatusNone","theImage": NSImageNameStatusNone],
        ["imageName":"NSImageNameShareTemplate","theImage": NSImageNameShareTemplate]
                            ]

    @IBOutlet weak var window: NSWindow!

    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return imageArray.count
    }
    
    
    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?{
        let view = tableView.makeViewWithIdentifier("myView", owner: self) as! KSImageTableCellView
        view.imageView.image = NSImage(named: imageArray[row]["theImage"] as! String)
        view.imageName.stringValue = imageArray[row]["theImage"] as! String
        view.imageNameConstant.stringValue = imageArray[row]["imageName"]  as! String
        return view
    }
}
You can download the source code for here NSImageNamed-AllSamples
Posted in Swift Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news