Thursday, December 18, 2014

Work with UIKit UIImage in XCode 6.1 Swift Playground

XCode 6.1 was one of the finalists of the Dr. Dobb’s Jolt Awards 2015: Coding Tools. The Playground introduced in XCode 6 is extremely useful to easily test your Swift code snippets.

However, there is some confusion about the possibility of working with some UIKit classes and check the results in the Playground.

You can create instances of many UIKit classes in the Playground and check the results at the right-hand side of your code snippets. In this case, I’ll provide you an example of how to create UIImage, CGRectMake, and UIButton instances. Then, I will set a title for the button, change the title’s color and finally set a background image.

After you create a new Swift Playground that targets iOS in XCode, select View | Utilities | Show File Inspector. Check the value for Resource Path below Playground Settings. In my case, the Resource Path is /Users/gaston/Documents/GastonHillarPlayground.playground/Resources. Thus, if you want to load an image within the Playground, just copy it to the folder specified in Resource Path. In my case, I just want to load an up arrow image, and therefore, I copy Arrow-Phone.png to the /Users/gaston/Documents/GastonHillarPlayground.playground/Resources folder.

Swift Playground that targets iOS in XCode, select View | Utilities | Show File Inspector.

Then, you can enter the following code and use either the Quick View eye or the Circle with the Plus (+) sign to check the visual results.

import UIKit

let image = UIImage(named: "Arrow-Phone.png")

let rect = CGRectMake(0, 0, 300, 100)

let button = UIButton(frame: rect)

button.setTitle("Up", forState: UIControlState.Normal)

button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

button.setBackgroundImage(image, forState: UIControlState.Normal)

The following screenshot shows the results of clicking the Circle with the plus (+) sign for many code snippets and the visual results displayed at the right-hand side. Don’t forget to copy the images to the Resource Path folder. It’s the easiest way to make the Playground display images.
Work with UIKit UIImage in XCode 6.1 Swift Playground

Wednesday, December 17, 2014

How to check Intel AVX2 support on Mac OS X

If you wanted to check whether your Intel CPU included support for Intel AVX1 (short for Intel Advanced Vector eXtensions), also known as AVX1.0, you could execute the following command on a Terminal window:

sysctl -a | grep machdep.cpu.features

However, if you execute the command on a MacBook Pro running OS X Yosemite Version 10.10.1, with an Intel Core i5-4278U, you will notice only AVX1.0 is listed in the features list:

machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C



You can check the official specs for an Intel Core i5-4278U CPU here, and you will notice the CPU provides AVX2 (indicated as AVX 2.0, and also known as Haswell New Instructions).

You have to run the following command on a Terminal window to check whether AVX2 is listed:


sysctl -a | grep machdep.cpu.leaf7_features

The results of executing this command in the configuration I mentioned before is the following:

machdep.cpu.leaf7_features: SMEP ERMS RDWRFSGS TSC_THREAD_OFFSET BMI1 AVX2 BMI2 INVPCID



As you can notice, the results include AVX2, and therefore, the configuration includes support for Intel AVX2 and you can use the necessary compiler options to generate code that takes advantage of this powerful instruction set.

Friday, December 5, 2014

High DPI in Windows 8.1: Checking the DPI Awareness with Process Explorer

If you invested in a high DPI laptop or a high DPI screen to enjoy crisp and clear text, you will be disappointed with some IDEs and other developer targeted applications that display both blurry text and images. As a developer, you spend a long number of hours reading documentation and hundreds of lines of code and your eyes will benefit from developer tools to be per-monitor DPI aware in Windows 8.1.

Unluckily, there are still too many developer tools that aren’t even system DPI aware and you cannot enjoy the benefits of your high DPI screen when working with them. If you see either blurry text or images in a window, you can easily check the DPI awareness of the related process with the Process Explorer utility. Notice the values of the DPI Awareness column in the following screenshot:


If you work with a multi-monitor workstation, you will have the best experience with the processes that indicate a Per-Monitor Aware value for the DPI Awareness column, as long as you have Windows 8.1 installed. So far, the latest version of Google Chrome (Version 39.0.2171.171m) is just system DPI aware (you will see the System Aware value in the DPI Awareness column for chrome.exe). Thus, if you drag one chrome window from one screen to another one that has a different high DPI resolution, you will not have the most possible crisp text and images in one of the screens.


If you are interested in diving deeper on high DPI displays in Windows 8.1 and how to develop apps that deliver crisp text and pictures on all the screens in which your application can be dragged to, you can read the two articles I published on Dr. Dobb’s that include many code samples and a complete explanation of the different modes in Windows 8 and Windows 8.1.