Getting Started with LEADTOOLS for iOS / macOS

Take the following steps to start working with the LEADTOOLS for iOS / macOS toolkit to create an iOS application demo that can run on either an iPhone/iPad simulator, or on an iOS device.

Note: A Mac OS X operating system (macOS 10.10 or later with Xcode 8.0 or later) is needed.

  1. Start the Xcode application. It can be found in the task bar, as shown below:

  2. If the Xcode application is not available on the task bar, start Xcode by selecting Launchpad from the task bar. Then type "Xcode" into the search bar and click the "Xcode" icon.

    Or search for it using the Spotlight application located on the top-right corner of the menu bar, or by using the Command + Space hotkey:

  3. In the Xcode startup dialog, click the Create a new Xcode project button.

  4. Clicking the button opens the Choose a template for your new project dialog box. This is used to specify the type of project to be created. Under the iOS templates select Application, then select Single View Application, then click Next.

  5. The Choose options for your new project dialog box will appear in order to specify the project name and options. Select Objective-C for the language, make your selections, and then click Next.

  6. A dialog box will appear in order to specify the location where the project will be saved. Select the location, then click Finish.

  7. Add the necessary LEADTOOLS frameworks that we need into our project. To start, select the project file in the project navigator pane to open up the project settings. Under the General tab, scroll down to the bottom and click the + button under the Linked Frameworks and Libraries section.

  8. Click the Add Other… button to browse the system for the LEADTOOLS frameworks. The iOS frameworks will be located in the Frameworks/iOS subdirectory of the location where you installed the LEADTOOLS SDK. Once you have located the frameworks, add the following to your project:

    • Leadtools.framework
    • Leadtools.Controls.framework
    • Leadtools.Converters.framework
    • Leadtools.ImageProcessing.Color.framework
    • Leadtools.ImageProcessing.Utilities.framework

    If you already had a group in your project entitled Frameworks, all of these new frameworks should have been added to this group. If you didn't already have this group, create it by highlighting all of the frameworks you just added, right-clicking (or Control + Click) the selected frameworks, and selecting the New Group from Selection menu option. You can then title the group Frameworks. Any frameworks you add to your application after this should automatically be added to this group.

    Next, add the LEADTOOLS license and key file to the project that was installed with the SDK. To do this, highlight the Supporting Files group and select File -> Add Files to … menu item (or use the Option + Command + A hotkey). The license and key files were installed in the Examples/Resources/License subdirectory of the LEADTOOLS toolkit installation.

  9. After adding the license and key file, add a new file entitled Prefix.pch to your project, and then click the file to edit it. It is here that the frameworks' header files need to be added so that they are global to the project. After opening the file, copy and paste the following code:

    #import <UIKit/UIKit.h> 
    #import <Leadtools/Leadtools.h> 
    #import <Leadtools.Converters/Leadtools.Converters.h> 
    #import <Leadtools.Controls/Leadtools.Controls.h> 
    #import <Leadtools.ImageProcessing.Color/Leadtools.ImageProcessing.Color.h> 

    You will also need to modify the following build settings:

    Other Linker Flags = "-lstdc++ -ObjC"

    Framework Search Paths = "<Path to your iOS frameworks>"

    Precompile Prefix Header = Yes

    Prefix Header = "<Path to your Prefix.pch file>"

  10. Build the project from Project -> Build or Command + B. Doing this makes Xcode compile all of the LEADTOOLS frameworks for use in all of the other files.

  11. Create the UI. To start, open the file entitled Main.storyboard. Using the Object Library in the bottom right-hand corner, drag and drop a UIView onto the view controller. Using the resizing handler, resize the view so that it occupies the entire controller except for 50 pixels on the bottom. Still having the new view highlighted, open the Identity Inspector and change the class type to LTImageViewer:

  12. Pin this view so that it will resize accordingly with different screen sizes/resolutions. To do this, click the Pin icon in the bottom right-hand corner of the toolbar and select the following pin options:

  13. Add three UIButton instances onto the bottom of the view controller, named Load, Flip, and Invert. These buttons should be in the bottom-left, bottom-center, and bottom-right corners (respectively). After adding them to the view, select the following pin and align options:

    Load:

    Flip:

    Invert:

    These are all of the constraints needed in order to have the viewer resize itself according to the device screen size/resolution.

  14. Add references of these objects to the ViewController class as well as hook-up action methods for the buttons. To do this, first open the Assistant Editor (View -> Assistant Editor -> Show Assistant Editor). Once the Assistant Editor is open, select the ViewController.h file from the top of the Assistant Editor Pane.

  15. Now that everything is set up, control-drag (Hold Control key and click) from the LTImageViewer in your storyboard to the ViewController class in the Assistant Editor. Upon releasing, a dialog appears that allows you to create and connect an outlet. Give the outlet a name, and then click connect:

  16. Repeat the same step for each button -- except this time, change the connection type from Outlet to Action:

    The actions for the buttons should have the following names:

    Load: loadImageFromAlbum Flip: flipImage Invert: invertImage

  17. Add a new method declaration with the following signature:

    - (void)showError:(NSError *)error;

  18. Add conformance to the following protocols for the ViewController class:

    • UIImagePickerControllerDelegate
    • UINavigationControllerDelegate
  19. Open the ViewController.m file and replace the class implementation with the following code:

    @synthesize imageViewer; 
     
    - (void)viewDidLoad { 
       [super viewDidLoad]; 
       NSError *error = nil; 
       NSString *developerKey = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LEADTOOLS.LIC" ofType:@"key"] encoding:NSUTF8StringEncoding error:&error]; 
       NSString *licenseFile  = [[NSBundle mainBundle] pathForResource:@"LEADTOOLS" ofType:@"LIC"]; 
       if (error != nil) { 
          [self showError:error]; 
          return; 
       } 
                     
       [LTRasterSupport setLicenseFile:licenseFile developerKey:developerKey error:&error]; 
                     
       if (error != nil) 
          [self showError:error]; 
                     
       self.imageViewer.newImageResetOptions = LTImageViewerNewImageResetOptionsNone; 
       self.imageViewer.sizeMode             = LTImageViewerSizeModeFitHeight; 
    } 
                     
    - (void)didReceiveMemoryWarning { 
       [super didReceiveMemoryWarning]; 
                      
       [self.imageViewer freeRasterImage]; 
    } 
                      
    - (IBAction)loadImageFromAlbum:(id)sender { 
       if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
          UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Your device does not allow using the Photo Library" preferredStyle:UIAlertControllerStyleAlert]; 
          [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]]; 
                      
          [self presentViewController:alert animated:YES completion:nil]; 
                      
          return; 
       } 
                      
       UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
                      
       imagePicker.delegate   = self; 
       imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
                      
       [self presentViewController:imagePicker animated:YES completion:nil]; 
    } 
                      
    - (IBAction)flipImage:(id)sender { 
       if (self.imageViewer.image == nil) // Nothing to do 
          return; 
                      
       NSError *error = nil; 
       LTRasterImage *rasterImage = [LTRasterImageConverter convertFromImage:self.imageViewer.image options:LTConvertFromImageOptionsNone error:&error]; 
       if (rasterImage == nil) { // Error occurred 
          [self showError:error]; 
          return; 
       } 
                      
       LTFlipCommand *command = [[LTFlipCommand alloc] initWithHorizontal:NO]; 
                      
       BOOL success = [command run:rasterImage error:&error]; 
       if (!success) { 
          [self showError:error]; 
          return; 
       } 
                      
       self.imageViewer.rasterImage = rasterImage; 
    } 
                      
    - (IBAction)invertImage:(id)sender { 
       if (self.imageViewer.image == nil) // Nothing to do 
          return; 
                      
       NSError *error = nil; 
                      
       LTRasterImage *rasterImage = [LTRasterImageConverter convertFromImage:self.imageViewer.image options:LTConvertFromImageOptionsNone error:&error]; 
       if (rasterImage == nil) { // Error occurred 
          [self showError:error]; 
             return; 
       } 
                      
       LTInvertCommand *command = [[LTInvertCommand alloc] init]; 
                      
       BOOL success = [command run:rasterImage error:&error]; 
       if (!success) { 
          [self showError:error]; 
             return; 
       } 
                      
       self.imageViewer.rasterImage = rasterImage; 
    } 
                      
    - (void)showError:(NSError *)error { 
       NSString *message = error != nil ? [NSString stringWithFormat:@"Error\nReason: @\nDescription@\nCode: %ld", error.localizedDescription, error.localizedFailureReason, error.code] : nil; 
                      
       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:message preferredStyle:UIAlertControllerStyleAlert]; 
       [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]]; 
                      
       [self presentViewController:alert animated:YES completion:nil]; 
    } 
                      
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
       [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 
                      
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { 
       self.imageViewer.image = (UIImage *)info[UIImagePickerControllerOriginalImage]; 
                      
       [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 

  20. Run this project which has the ability to load, flip, and invert images:

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize