Instant OpenCV for iOS is a practical guide, showing every important step for building a computer vision application for the iOS platform. It will help you port your OpenCV code, profile and optimize it, and then wrap into a GUI application. Each recipe is accompanied with a sample project, helping you focus on a particular aspect of the technology.
In this recipe, we'll provide all the necessary steps to set up your environment and run a "Hello World" application on a device. Development for the iOS platform may seem to be difficult in the beginning, because the list of prerequisites is somewhat large. We'll provide important links for those who are not familiar with Mac/iOS development, Objective-C, and Xcode. If you're already familiar with the iOS development, you can skip this recipe.
Apple has established a very rich and sound ecosystem for developers, where each component is tightly integrated with others. Once you're familiar with its basic rules and principles, you'll be able to switch between different types of projects easily. But it may take some time to familiarize yourself with the tools and frameworks. And the very first prerequisite for iOS development is a Mac OS X workstation or laptop, and you cannot use other operating systems. It is also highly recommended to use the latest available version of the OS and tools, as some new features are not backported to older versions. Currently, the latest version is the Mac OS X 10.8, also known as Mountain Lion.
Secondly, to run some examples from this book, you will definitely need an iOS device, because iOS Simulator lacks camera support. You should also know that Simulator executes x86 native code, while real iOS devices are running on ARM. This difference will not allow you to understand the actual performance of your application, which is usually important. You can use a simple device such as iPod Touch, which is quite cheap and may be useful not only for development! But of course, we recommend you to find one of the latest iOS devices; currently these are iPhone 5 and iPad 4. The iOS version should be 6.0 or higher.
Actually, you can use Simulator for experimenting with many examples in this book. It is also a good chance to test your application on a tablet if you only have a phone, or vise versa. But please note that samples that need camera will not work. Every time you need a real device, we will mention that in the beginning of the recipe.
When you have all the hardware, you'll need to install Xcode—the cornerstone of all Mac-centric development. You will need Version 4.6.3 or higher. We recommend installing it together with the command-line tools, so you'll have access to compiler and some other tools from the Terminal.
We're almost ready to start development, and you can actually proceed to the following How to do it... section if you're going to use Simulator. But if you're going to use a real device now or later, there is one more step. In order to run your application on a real device, you have to become a registered Apple developer (which is free), and you will also need to subscribe for the iOS Developer Program, which will cost you $99 per year. This may look too high for the opportunity to play with your little applications, but this is how Apple verifies that you're serious, and that you will do your best to create a decent app. Also, it gives you access to all beta software from Apple, related to iOS, which is very important from a developer's perspective. Registration procedure is described at https://developer.apple.com/register/index.action, and the page about the "iOS Developer Program" is at https://developer.apple.com/programs/ios/. Finally, you need to register your device according to this instruction found at http://bit.ly/3848_DeviceProvisioning.
Source code for this recipe is available in the Recipe01_HelloWorld
folder in the code bundle that accompanies this book.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
So, now we're ready to create our first "Hello World" application, and it's going to be in Objective-C. The following are the steps required to achieve our goal:
Let's implement the described steps:
ViewController.m
file, which can be found in the Project Navigator Area on the left-hand side. We're going to add a simple logging to the console, and an alert window. In order to do that, please edit the viewDidLoad
method, so it looks like the following code:- (void)viewDidLoad { [super viewDidLoad]; // Console output NSLog(@"Hello, World!"); // Alert window UIAlertView *alert = [UIAlertView alloc]; alert = [alert initWithTitle:@"Hey there!" message:@"Welcome to OpenCV on iOS development community" delegate:nil cancelButtonTitle:@"Continue" otherButtonTitles:nil]; [alert show]; }
The NSLog
function, which we added first, is intended for logging simple text messages, similar to the printf
function in the C language. You can also see that our string was preceded with the @
character, which is used for implicit conversion to the NSString
object. Just like printf
, NSLog
allows you to print values of multiple variables with proper formatting, so this function is quite helpful during debugging and profiling of your application.
While the NSLog
function is useful for logging, you should not keep these logs in the production code because like any other IO procedure, it has its cost, which may negatively affect the performance. So, you should use conditional compilation or something else to remove all debug logs from the release code.
The next code block shows how one can create a simple message window with some notification. We are not going to use UIAlertView
in later recipes, but this is a good occasion to get familiar with creating objects and calling their methods in Objective-C. The first line shows how a new object may be created. Here, the alloc
method is called that was inherited by UIAlertView
from its parent NSObject
class. Next, we're calling the initWithTitle
method, passing necessary arguments to it. This method returns a newly initialized alert window. Finally, we call the show
method to display the window with our message.
Please note that we've reformatted the code for readability, and we've even created one surplus temporary object. It was possible to call the initWithTitle
method of a newly allocated object, so in most cases, you will likely meet the initialization code as shown in the following code snippet.
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hey there!" message:@"Welcome to OpenCV on iOS development community" delegate:nil cancelButtonTitle:@"Continue" otherButtonTitles:nil];
That's all for this recipe, but if you feel that you need some more introductory information on iOS development in general, we'll provide you with some pointers. Later, we will focus mostly on OpenCV-related aspects of programming, so if you want to better understand Apple's tools and frameworks, you should spend some time studying other resources.
We encourage you to "sharpen your saw" and read more about Xcode. Refer to the official documentation at https://developer.apple.com/xcode/. For example, you can create several Simulators to test your application on different types of devices. Navigate to Xcode | Preferences, and then go to the Downloads | Components to see the available options. Many useful tips on Xcode and sample code examples are available in the iOS Developer Library at http://developer.apple.com/library/ios/navigation/.
One of the important things you should know about Objective-C is that it is a strict superset of C, and you can mix it with C. Because OpenCV is written in C++, we have to use Objective-C++, which allows to use a combination of C++ and Objective-C syntax and also allows you to reuse your C++ code or libraries (as we do with OpenCV). Nonetheless, Objective-C is the main language for iOS, so you should get familiar with it in order to use the available libraries and frameworks effectively.