Wiki

Clone wiki

SimpleDrawing / Home

Introduction

As the name implies, SimpleDrawing is a simple open source drawing app for iOS. You can try the app, for free, on the Apple App Store. I created SimpleDrawing to serve as an example of how to handle the basic drawing features on an iOS device, including:

  • Tools such as pen, brush, bucket, line, text, rectangle, ellipse, and eraser
  • Individual tool settings such as primary and secondary color, line width, transparency, and font size
  • Importing photos from the photo album
  • Multi-level undo and redo operations
  • Layers with individual levels of transparency
  • Sharing via mail, Twitter, camera roll and more

The drawing functionality of SimpleDrawing can also be used within your own applications with minimal effort.

simple_drawing_iphone_medium.png

The design of the code also makes it easy to add your own tools by inheriting from SDDrawingTool and overriding (usually) only one method.

Installation

It is possible to use the drawing functionality of SimpleDrawing within your own project. First, add the contents of the DrawingLib folder to your Xcode project by drag-and-drop. Next, add the required frameworks: Quartz, Twitter, and MessageUI. Finally, disable ARC using the -fno-objc-arc flag for the files BGRSLoupeLayer.m, BSBrightnessSlider.m, and RSColorPickerView.m.

Usage

Once the required files are included in your project, with just a few lines of code you can display a view that lets you browse and create new drawings:

// Implement the SDDrawingViewControllerDelegate methods. Your interface should implement the protocol SDDrawingViewControllerDelegate
- (void)viewControllerDidSaveDrawing:(SDDrawingViewController*)viewController {
    NSString *drawingImageFile = [[viewController photoDirectory] stringByAppendingPathComponent:kSDFileFlatDrawing];
    NSLog(@"Processing drawing %@", drawingImageFile);
    [self dismissViewControllerAnimated:YES completion: nil];
    UIImage *img = [UIImage imageWithContentsOfFile:drawingImageFile]
}

- (void)viewControllerDidCancelDrawing:(SDDrawingViewController*)viewController {
    NSLog(@"Cancelling %@ drawing", viewController.drawingID);
    [self dismissViewControllerAnimated:YES completion: nil];
}

- (void)viewControllerDidDeleteDrawing:(SDDrawingViewController*)viewController {
    NSLog(@"Deleting %@ drawing", viewController.drawingID);
    //[self dismissViewControllerAnimated:YES completion: nil];
}

- (IBAction)handwritingButtonClicked:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NAVIGATE_TO_DRAW"];
    //instantiate the view controller
    NSBundle *bundle = [NSBundle bundleForClass:[SDDrawingsViewController class]];
    UIStoryboard *storyboard;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        storyboard = [UIStoryboard storyboardWithName:@"SDSimpleDrawing_iPad" bundle:bundle];
    } else {
        storyboard = [UIStoryboard storyboardWithName:@"SDSimpleDrawing_iPhone" bundle:bundle];
    }
    SDDrawingsViewController *viewController = [storyboard instantiateInitialViewController];
    viewController.delegate = self;
    //present the view controller
    [self presentViewController:viewController animated:YES completion:nil];
}

It is also possible to customize aspects of the main drawing view within your application by making use of the drawingViewCustomization Block property:

//use the drawingViewCustomization block to customize the SDDrawingViewController view
drawingsViewController.drawingViewCustomization = ^(id sender){
        
    SDDrawingViewController* drawingViewController = (SDDrawingViewController*)sender;
        
    //increase margins for the title items to compensate for custom gunmetal theme
    const int offset = 30;
        
    CGRect itemFrame = drawingViewController.titleButton.frame;
    itemFrame.origin.x += offset;
    itemFrame.size.width -= offset * 2;
    drawingViewController.titleButton.frame = itemFrame;
        
    itemFrame = drawingViewController.titleTextField.frame;
    itemFrame.origin.x += offset;
    itemFrame.size.width -= offset * 2;
    drawingViewController.titleTextField.frame = itemFrame;
        
};

Using the SDSimpleDrawing_iPad storyboard presents the user with a suitable iPad UI:

simple_drawing_tools_ipad_small.png

Apps Using SimpleDrawing

SimpleDrawing uses only public API's and should not cause your application to fail submission to the Apple App Store. The following iOS applications make use of the SimpleDrawing source code and have been approved by Apple:

License

Copyright (C) 2012 Nathanial Woolls

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Updated