Drawing tool in iOS ( openGLES or UIBezierPath )

New to the problem of making a drawing/painting tool for an iOS application many developers get confused on using either OpenGLES or UIBeizerPath. Sometimes sample code on apples developer website GLPAINT  points many developers to use OpenGL Views for making paint layers for an application. A major issue with this that the developers do not understand is that OpenGL Views leave very heavy memory footprints and on devices like ipad1 having 24mb of memory to use per view , this can be a pain.

Trying to make a transparent openGL view which allows the user to paint on a transparent canvas with the background views visible is a problem I faced. Often crashes start occuring once you add multiple openGL views on a single view i.e adding multiple transparent paint canvases on a single view. These crashes are shown to be occuring because of the application receiving memory warning and the delegate “didReceiveMemoryWarning” being called.

After spending a lot of time with OpenGL , trying to figure out  a way to reduce the memory footprint (using instruments in Xcode), one gives up. The solution thus thought turns out that ipad1 cannot support multiple paint layers and the functionality has to be restricted for user to be able to add only a single layer per view, which indeed is not true.

Now comes the boon of UIBeizerPath class. This class introduced in iOS 4 SDK  makes it a lot easier for a developer to make multiple transparent paint canvases and add it to a single view without having a high memory footprint.

Steps on how to make use of UIBezierPath :

-       Add a new UIView class to the project , name it paintCanvasView.

-       Add two objects  in header file,

-       Initialize these objects in the init method by overridding it.

-       Override drawRect

-       Implement Touches Began

-       Implement Touches Moved

That is it.

You can use this PaintCanvasView Object in your code like any other UIView Object.  I hope this post would help some developers who are unaware of UIBeizerPath class and its uses. A further implementation of this is adding pattern to the rendered strokes. This pattern can be rendered on the strokes using pattern image , assuming pattern.png. Replacing the bColor with a pattern image in init method would do this trick.

 

Update 16May’12

Adding infinite undo and Redo managers:

-   Add two arrays to paintCanvasView.h

-   Add two methods to paintCanvasView.h

-   Allocate the arrays in init method

-   Modify drawRect in paintCanvasView.m to draw from pathArray

-   Add bPath to pathArray  in touchesBegan

-   Define undoButtonClicked and redoButtonClicked to modify pathArray as desired.

That is it.

There is no rocket science to make an infinite undo manager for paint or sketch. One can simply maintain two arrays, buffer and path. By easily maintaining all items removed from path array in buffer array, redo functionality is achieved.


VSCircularDial - open source iOS component

VSCircularDial is an UIView enhancement which provides you a 360 degree scrollable dial. This component can be dropped into your project and used as it is or can be customized as per your need.

It can be downloaded from github.

Features

1. Easy to use, just like normal UIView.
2. Fully customizable
3. Optimized for different resolutions, including iPhone, iPad and iPhone 4 (Retina Display)
4. Callbacks can be implemented to call specific methods on rotating the view.

Usage

Very similar to UIView. For VSRotatingView, you can initialize with "new" method:

VSRotatingView *rv = [VSRotatingView new];
[view addSubview:rv];

Initial Steps

1. Download and add the complete "VSRotatingView" folder in your project from here.
2. You need to add three frameworks in your project - QuartzCore, AVFoundation and Security.
3. Add #import "VSRotatingView.h" to the top of view controller in which you are going to use the above code and you are ready to go.
4. Function "- (void)viewCirculatedToSegmentIndex:(NSUInteger)index;" can be called in its delegate which can be used to know when view is beeing rotated and which segment is the current selected one.

Customizing this view is a lot simple. Variables could be modified in VSConstants.h to get desired functionality and the main image of dial can be replaced easily with image named "pain-cycle.png". Complete code can be accessed in VSRotatingView.h/.m files and full modification is possible.

Two demo applications are included in the project. Project#1 shows the default functionality of this component. Demo project#2 shows how you can customize this view and show a 360 degree view like the one below:

This component can be used to make various applications like wheel of fortune, etc.


Apple Deprecated Access to UDID

OK. So some of you might have heard that Apple has deprecated access to UDID.

Well, that's true....sort of...but what on earth does it mean.

Let's find out.

Every iPhone, iPad, and iPod touch has an identifier unique to that device.

It is called a UDID which stands for Unique Device ID.

Some apps use it directly or some create a hash using it to uniquely identify the user/device.

They use a property called "uniqueIdentifier".

Apple is now deprecating the method that is used to access the UDID of a device from within the app.

In other words, this method is slated for removal in some time and the apps already using this need to make provisions to transition off it.

Here's what they say in the documentation.

Now how does Apple's decision affect the stakeholders.

As for end users, it doesn't affect them at all.

They have remained oblivious to the presence of UDID and would continue to do so.

There have been some blog posts around users screaming that their privacy is being compromised just because the app is sharing their UDIDs.

Baah! I dont see why that should be a big privacy concern.

There is a significant change for developers / app providers and advertisers however.

Some applications needed to identify individual users, say for subscription purposes.

Either they required all users to have an account at a central location, or they had to identify the user remotely on his device.

UDID helped in the latter scenario.

Using UDID and a little analytics code, the app provider could get valuable insights into the usage of the app.

However, with UDID going away, each app would have to create its own identifier for each device.

While that alone would not be a problem, the issue would be in reconciling this data with the previous data that used other means of identifying a user (UDID or a hash based on it).

So unless the app developers can find out a cunning way to reconcile the data, they would be starting afresh essentially.

Because this analytics data was the advertisers' holy grail, they have nothing to cheer about either.

But why is Apple doing this anyways?

Well, "Who is John Galt"?

[I am reading Atlas Shrugged by Ayn Rand of late and this phrase stuck.]

Some highlight privacy concerns, including law suits, raised by some users of late (which I find frivolous).

However, there might be a hidden agenda of Apple in seeking to discontinue use of UDID.

Apple gets a share of the revenue generated by selling paid apps on its store.

It also gains from paid subscriptions.

Normally, when you buy a paid app, you can download it on another device at no extra cost by signing in with the same Apple id.

Apple gains on the first purchase, but subsequent installs don't add to its coffers.

However, there are some paid apps that charge you for every install.

So if you paid a dollar when you bought and installed the app on your iPhone, you might have to pay some more to install the app on a new device, say your iPad.

This model is made possible only by identifying the devices individually and not necessarily the user alone.

Doing away with UDID would do away with this model too.

Could that be the reason?

Well, we would see.

Enough of doom and gloom.

There's still a way out.

If you don't really require any feature offered by iOS 5 SDK, you can continue to use any lower version and still access a device's UDID.

The access is going to be deprecated only for iOS 5 SDK.

So unless you want to use iOS 5 SDK, you are good.

So long.


What's New in iOS5

iOS5

This is the first major upgrade to iOS ever since it was rechristened from iPhone OS.

Apple's iOS 5 page says that they are "taking iOS to a whole new level".

Scott Forstall, SVP iOS Software, says that this is the "widest and most extensive update for iOS, ever".

The update boasts of 200 new updates.

Let's look at the new features to see if that is really true or is it just old wine in a new bottle.

I am able to review some of these on an iPad running iOS 5 Beta 7.

1. Notifications

This is not a new feature. iOS users are already familiar with notification pop-ups and badged icons. What is new is the way these notifications are lending themselves to the user.

With the device in an unlocked state, you can drag down the notification bar on top and see a list of notifications from various apps. Tapping any would take you straight to that app.

While iOS users may drop their jaw on this, Android users are only yawning as this was already on Android since version 1 !!! (do I hear someone shouting "copy-cats")

Additionally, the iOS notification drop-down also shows weather and a stock ticker.

Even in a locked state when the device shows a list of notifications, you can swipe a notification directly and open its app instead of having to unlock the device first. Now that is something that would make Android users jealous.

2. Newsstand

Apple had caused a lot of heartburn to publishers earlier this year over its seemingly atrocious terms of engagement.

However, after the two parties agreed to mutually respectable terms, it was the end user who emerged as a winner.

Newsstand is a single repository of all your newspaper and magazine subscriptions purchased from App Store.

The magazines and dailies are downloaded in the background and are available for offline reading.

Simulating your book shelf, the exploded view of the newsstand shows you the cover of each magazine and daily that you have subscribed to.

3. Twitter

Twitter app for Apple was already available since but what is new is the OS wide support for it.

Integrating Twitter into iOS and offering it as a single sign-on is a smart move by Apple and great news for the Twitterati.

Now you can sign-on with Twitter and login to any other application permitting a Twitter login without having to specify your credentials again.

Not to mention, that the OS wide integration allows you to Tweet from a lot of native applications like Photos, YouTube, Safari, Maps, etc.

Additionally, you can complement your contacts information with their Twitter information like photo, @username, etc.

4. Safari

Mozilla's Firefox has always been my personal favourite (at least on a PC*) but the additions iOS has done to Safari really tempt me to switch.

When you are reading an article (blog post, news article, etc.) on Safari and switch on the Reader mode from the address bar, you see the article freed of all the clutter (like side bars, ads, related stories, all the junk that web developers like us put in a page). The article is displayed in a neat (I am tempted to say "sexy") fashion with Serif fonts, a la hardcopy publication. Just focus on the article without any distractions.

Reading list is another interesting addition. It allows you to save articles for later viewing. And more importantly, this list is accessible across devices by using iCloud. So you can view articles on your iPhone while commuting home that you had saved earlier on iPad while in office.

And to top the Safari cake is the cherry of tabbed browsing. Switch among multiple tabs the same way you would on your PC*.

5. Reminders

I have always had trouble managing my to-do lists, at least on a PC. Simple text document, Excel sheet, Ta-da lists, I have tried it all.

But this simple application can be a solution to all your woes about managing to-do lists, at least on iOS.

You can create new lists and quickly add list items. Check them off as you complete them.

View all completed list items in a separate list and in the list you created.

You can mark a date and time to be reminded about an individual list item (without which it would not make sense to call the feature as "Reminders").

Here comes the best part about reminders: in addition to specifying time and date, you can also specify place. That is, be reminded of a chore when you arrive at a place (pick up groceries from supermarket) or when you leave a place (call your wife when you leave from office so that she can be prepared with a new set of chores when you reach home).

And you can sync these reminder with your other Apple devices using CalDAV or iCloud or with your Outlook using Microsoft Exchange.

6. Camera

Now Apple claims to have the most popular camera among mobile phones and as popular has regular cameras using Flickr as a reference for this statistic.

Even if that were to be true, the new features would only serve to bolster iPhone's camera's position.

The chances of missing a moment worth capturing are even less with the quick access to camera even from the lock screen.

Just double click the home button to reveal the camera button and dive straight into the camera app.

And click like a regular camera with your iPhone held in landscape mode and using the volume-up button. :)

Also you can view grid lines on your screen so that you can align the picture even before taking it.

Want to zoom in, just pinch to zoom!

Another great feature which I doubt if any other mobile camera has is an AE/AF Lock.

Now I am not a photography expert but as I understand, on iPhone, if you select an area and lock the AE/AF (auto exposure, auto focus) and even pan a little, the exposure and focus of the selected area would not change despite the change in background light.

I would have to check this on an iPhone running iOS 5 but as I gather from Scott's# demo, it seems to work.

7. Photos

All that beautiful pictures can me made too look even better by some handy tools now offered by the Photos app.

I hate it when my eyes in the photo appear red as if I am a Sith lord. You need not make your loved ones look like that if you use the red-eye reduction tool.

Then you can also crop, rotate, organise into albums the photos you took on your iPhone on the phone itself.

8. Mail

While the mail app on iOS devices used to appear like makeshift mail, it is going to become lot better and would come close to appearing like the mail on Mac (well, almost).

With the addition of rich text editing (bold, italics, underlines, etc.) and indentation, mails sent from iOS devices would look lot more like their Mac cousins.

Besides, you wont have to type again if you mentioned a contact name by mistake in CC instead of To or BCC. Just drag them to the other field.

Messages can be flagged / unflagged on the device itself, searching would include the mail body in addition to the to, from, and subject fields, swipe to view/hide the inbox, yada yada yada.

But one feature really made me skip a beat (I actually said "Whoa" when I saw the demo) is the split keyboard on iPad

You can split the keyboard and drag it upwards so as to type by just using your thumbs while holding the iPad. Awesome!

9. PC Free

This is actually a goood one (that wasn't a typo, I use the ooo sound when I really like something, like "hey, that girl is goood")

The pre-requisite of having a PC to sync your device with has been done away with.

The iOS device can be the only computing device in your home for all you care.

Activate, update it over-the-air. Not to mention that updates are now delta-updates so you just download what has changed, not everything including the kitchen sink.

10. iMessage

Well, I am not saying that Apple is trying to emulate the success that BlackBerry achieved through its BlackBerry Messenger, but...umm...yes, I think I am saying that.

But its a great feature to have anyhow.

iMessage allows you to exchange messages over 3G/Wi-fi with your friend if he/she also has an iOS device.

iMessage is built into the existing messages and allows you to exchange photos, videos, group messages, and of course simple texts.

You can receive delivery receipts (no more excuses like "I never received your message"), read receipts (or "I didnt read your message") and even get to know when the other party is typing a message.

Besides, these messages are synced across all your iOD devices so you can resume the conversation from your iPad if, for example, your iPhone got run over by  a truck.

11. Wifi-Sync

This is another feature that aims at making your iOS device truly wireless.

No need to plug in your device to your PC to sync with iTunes. A shared wi-fi connection between your device and the PC would handle the syncing.

But, I am wondering what would be left of the poor battery if you are syncing a lot of audio and video.

These were some of the new features that iOS offers to the end users.

But even iOS developers have a lot to cheer about.

With more than 1500 new APIs, developers can unleash their creativity and develop really useful apps (and some weird ones too).

Apple claims to have done significant enhancements to Xcode, Instruments, and Simulator.

Then they have incorporated the CoreImage framework from Mac OSX to iOS that allows the developer to weave some real magic in their apps that handle images (red-eye reduction for example).

Hope you enjoyed reading the post.

Stay tuned for more (especially my soon to come blog post on Android).

Feel free to comment on this post or you can reach me at rajat(at)vinsol(dot)com.

* The term PC has been used to differentiate desktop computers, laptops from mobile iOS devices.
# iOS5 introduced and demoed by Scott Forstall, SVP iOS Software

References

1. Keynote Video: http://www.apple.com/apple-events/wwdc-2011/
2. iOS Video: http://www.apple.com/ios/ios5/gallery.html#video-ios
3. Features: http://www.apple.com/ios/ios5/features.html
4. Title Image Source: http://images.apple.com/ios/ios5/images/overview_hero.png