Wednesday, May 30, 2012

iOS Paint tool using UIBezierPath

I was going through how to use UIBezierPath and what are its possible uses.. and I must say it is one hell of a convenient tool! Ofcourse you can draw normal drawing with it as shown below and use your app as sketching pad in addition to that it adds so much convenience with the undo and redo action that I fell in love with the use of UIBezierPath. You can have simple drawing as well as pattern based brush tip. It is so easy to use this as pencil tool that I don't even have words !

You can initialize the UIBezierPath as following
UIBezierPath *myPath = [[UIBezierPath alloc] init];
myPath.lineWidth = 10;
brushPattern = [UIColor redColor]; // This is the color of my stroke
Then you have Touch methods which handle and track the coordinates of your touch. When your touch begins on the screen, you ask UIBezierPath to move to that touch point.
UITouch *myTouch = [[touches allObject] objectAtIndex:0];
[myPath moveToPoint:[myTouch locationInView:self]; 
As you move your finger around, you keep adding those points in your BezierPath in TouchMoved method by following
UITouch * mytouch = [[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
As we need constant refreshing of the screen, so that as soon as we draw it appears on the screen, we refresh the UIView subclass by calling following method in TouchMethod so that as soon as there any change in the BezierPath, it is reflected on the screen.
[self setNeedsDisplay];
Talking about drawRect Method which does all the drawing for you, you need to set the color of your stroke (stroke color means the color with which painting will be done on screen) on screen and also the blend mode. You can try different blend mode and see the result.
[brushPattern setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

Pattern Brush.. it just changes the custom pattern color of the paint brush and allows you to use a custom image-pattern to be used instead of any specific color (which was red in our case)

myPath.lineWidth = 10; //This can help you in setting width of the stroke/brush
brushPattern = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"pattern.jpg"]]; // You can set pattern in your color of your brush so that the stroke will appear with pattern of your image

Adding Undo/Redo feature

We don't have to do much after this step for adding the undo/redo feature in the existing code.
Add following lines to header (.h) file
  NSMutableArray *pathArray;
  NSMutableArray *bufferArray;
- (void)undoButtonClicked;
- (void)redoButtonClicked;

First, we declare two NSMutable arrays, one each for 'undo' and 'redo' steps. Then we declare two methods that gets called when we want to undo/redo.

Go to implementation file (.m) and lets start be looking into touch delegate methods:
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    myPath = [[UIBezierPath alloc] init];
    myPath.lineWidth = 10;
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath]; 
Here, whenever a new touch is detected, we initialize a new UIBezierPath and add it to "pathArray" (for undo steps)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self];
    [self setNeedsDisplay];
Next, each time a movement is detected, then we call 'setNeedsDisplay' and redraws the view which in turn calls 'drawRect' method
- (void)drawRect:(CGRect)rect

    [brushPattern setStroke];
    for (UIBezierPath *_path in pathArray)
        [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
Since, 'pathArray' contains all the UIBezierPaths, it loops through each of them and paints them on screen.
Below are the actions that gets called upon when we click on undo/redo buttons:
- (void)undoButtonClicked {
   if([pathArray count]>0))
        UIBezierPath *_path = [pathArray lastObject];
        [bufferArray addObject:_path];
        [pathArray removeLastObject];
        [self setNeedsDisplay];

- (void)redoButtonClicked{
    if([bufferArray count]>0)) {
        UIBezierPath *_path = [bufferArray lastObject];
        [pathArray addObject:_path];
        [bufferArray removeLastObject];
        [self setNeedsDisplay];

When undo button is clicked, we remove the last object from pathArray and save that object to 'bufferArray' - which stores all UIBezierPath for redo.
And when redo gets clicked, we remove the last object form 'bufferArray' and adds it to the 'pathArray'.
We ask the view to redraw itself again after performing any one of the above stuff.

That's all. This short code snippet lets you perform touch sensitive paint on iOS devices with properly working undo/redo functionality.

The complete code of above tutorial can be found on github.

Thursday, May 17, 2012

101 Reasons not to buy Windows Phone 7.5

Windows Phone is a mobile operating system developed by Microsoft, and is the successor to its Windows Mobile platform, although incompatible with it. Unlike its predecessor, it is primarily aimed at the consumer market rather than the enterprise market. Although Microsoft is doing every bit to popularize its new mobile OS, including new Metro style UI, "Mango" update, upcoming update to "Apollo - WP8", partnership with Nokia, still this Windows Phone 7.5 seems not to make itself a huge hit in the market which has already been captured by Android and iOS.

WP 7.5 (Mango) is already in its second generation but it still has many restrictions, limitations and serious usability issues. This is a list of its shortcomings as compared with other smartphone OS in the market. We are not comparing special features or abilities but those which have become the norm for all smartphones and even dumb phones. An Android, Symbian or MeeGo phone would have almost none of these shortcomings. An iphone share some restrictions with WP7.5 but is still miles ahead. Even the Nokia N9 running the first generation MeeGo OS is far better with none of the disastrous issues which impact on the sheer usability of the phone.

1. No true multitasking - apps are frozen in the background.
2. No Divx/Xvid video codec support. Zune will convert with loss of quality.
3. No mass storage mode.
4. No micro-SD card support.
5. Only support up to 16GB storage .
6. No filemanager. Directory system is totally opaque.
7. Need Zune to transfer files. Zune will only transfer photos, videos & music. All other files need to email/upload to yourself.
8. Your contact details are automatically uploaded to cloud service whether you like it or not.
9. Limited to 800x480 resolution.
10. No wi-fi hotspot.
11. Cannot use any MP3 file as ringtone except those with strict constraints.
12. Cannot set static IP address so no connection to ad-hoc networks.
13. No VPN support for this “corporate enterprise” phone.
14. Cannot sync with Outlook.
15. Totally closed OS, cannot sideload apps outside MS Marketplace.
16. System font size cannot be changed.
17. Images and photos cannot be renamed in the phone.
18. Windows Live ID account cannot change country once set.
19. No centralized notification page.
20. Alarm clock cannot work when phone is turned off. All Nokia Symbian and Meego phones can do this.
21. The idle screen is completely blank and cannot display time or notifications.
22. Only photos allowed as email attachments, documents not allowed.
23. Bluetooth cannot stream to car audio devices (no rSAP).
24. Cannot stream audio from video to Bluetooth devices (No A2DP).
25. No support for full on-device encryption required for secure applications like mobile banking and online payment.
26. Cannot use Bluetooth keyboard.
27. Cannot silence ringtone or alarm by flipping the phone.
28. Very limited customization option.
29. Ugly 2D flat interface. Oversized fonts for headings treat you like an idiot.

30. No always visible status bar for battery life, signal strength, carrier, 2G/3G wi-fi, Bluetooth on.
31. Taskmanager has no option to shut down apps you don’t want running in the background.
32. Search and Back button cannot be de-activated in apps or games and easily touched by accident which interrupt your user experience.
33. Lockscreen need to be activated to show missed call/sms notification.
34. No way to close an app except pressing back button all the way to the first screen.
35. Tiny fonts in messages is very hard to read for those over 45.
36. Cannot edit playlist or create new playlists from phone.
37. Cannot delete a song from phone.
38. Cannot search your music collection on the phone.
39. Cannot close music player, can only pause. Music player on lockscreen will stay until you reboot.
40. No progress bar for current track playing and no indication which track in an album is playing
41. Cannot lock screen orientation.
42. Online and phone contacts are mixed together with no ability to filter.
43. Cumbersome dialing with no smartdial. No way to create a ‘Favourites’ list of phone contacts
44. Cannot save draft sms messages.
45. Call history only show phone number type. If a contact has multiple phone nos. for a type the number used is unknown.
46. Cannot recognize phone numbers in sms or email to save or use as calling number.
47. Text messages can only be deleted one by one.
48. Cannot select multiple pictures for deleting, sending or uploading. They must be done one at a time.
49. Notification for recurring events is only given once and none thereafter.
50. Apps are listed alphabetically with no way to group by category. Can be hard to find if you don’t remember the name.
51. Calendar scheduler has no weekly view and monthly view is non-zoomable.
52. No peak/off-peak scheduling for email downloads.
53. If both wi-fi and data connection are available which one it chooses to use is unpredictable. User experiences don’t agree with Microsoft that it “typically” choose wi-fi over 3G.

54. No video call.
55. No haptic feedback for keyboard.
56. No Swype.
57. No flashplayer support.
58. No support for Java apps.
59. No call recording or app to do it.
60. No call blocking or app to do it.
61. No screenshots or app to do it.
62. No auto wallpaper changer and no app to do it.
63. Totally locked down os means apps which interact directly with hardware not allowed.
64. No equalizer for Zune music player.
65. FM radio doesn’t work on speaker.
66. IE browser has no text reflow, no download capability and no offline reading.
67. No 3rd party browsers allowed except those based on IE.
68. No built-in pdf file reader, need to buy app.
69. Cannot open zip or rar files received as email attachment.
70. Cannot send or receive video by MMS.
71. Office Mobile has much less features than 3rd party Office editors like SmartOffice, QuickOffice and Polaris. Shame on you, Microsoft!
72. No native Google maps and Bing maps is useless for most countries outside U.S.
73. Email time stamp does not show the year.
74. Wide difference in apps availability in different markets and users locked to one market.
75. Not all Bing features available outside U.S.

76. One volume control for all functions including media playback, ringtones, alarms, notifications.
77. Wi-fi disconnects when screen sleeps. Too bad if you are in the middle of a download.
78. If you receive a text message when talking on the phone an audio alert will blast your ear at the full volume set. All other phones will give a soft beep.
79. Can only enter one mobile phone no. for each contact. Mobile nos. entered in other fields will not accept sms.
80. Cannot send/receive MMS without enabling 3G data connection. MMS does not use 3G data.
81. Phone cannot be charged when off.
82. Need to be plugged in to wall charger to sync wirelessly (a funny definition of wireless).
83. Volume up/down buttons cannot operate when screen is off.
84. Phone will wake up and display sms content on lockscreen when locked – a privacy violation.
85. Phone can be rebooted without unlocking to bypass security lock.

86. No bluetooth file transfer.
87. Cannot handle USSD codes necessary for prepaid users to obtain services.
88. Cannot save new contacts from call history.
89. Cannot sms contacts from call history.
90. Cannot show call duration in call history.
91. Cannot delete individual calls in call history.
92. Call history does not group calls by contact.
93. Call history does not show the time of call for calls older than current day.
94. Cannot set custom sounds for different types of notifications.
95. No ringer profiles.
96. No silent option (no vibrate and no ring).
97. Cannot send/receive contact as a csv file.
98. Cannot backup sms to PC.
99. Cannot save contact to SIM card.
100. Cannot change alarm ring tone or use a MP3 file.
101. Cannot set alarm snooze interval.