Wednesday, April 27, 2011

How to check memory leaks in XCode 4 ?

You need to go to Product>Profile on the top menu of XCode 4 as shown in screenshot below.

Then you will see following window, you can just choose Leaks and Press Profile button, it will start the Instrument and start tracking leaks and allocation.

As soon as you press Profile, it will start Instrument and the simulator/device(You can change in the settings of the Instrument whether you want to run it on simulator or device) will start running the app.
You scroll through various pages on your app. While doing so,  if you view the red line in Leaks windows of Instrument as shown below then you have memory leak detected ! You can move the dotted line with slider to find out how many bytes leaked at what time in a small yellow pop up.

When you double click the red thick line of memory leak as shown  above , in the lower portion of the Instrument you will be able to see all the leaks and related info. If I want to find detailed info about a particular leak,  in this tutorial as you can see the blue highlighted line of leak, I am going to inspect about that leak hence I will just click the arrow which comes up in its address field.

This will refresh the window and tell you the details with  Responsible Caller column in the end of the table , it gives the specific method  name where the leaks has occurred. If you double click the row, it will open that corresponding .m file where that method has been implemented.

Now you can see that the line is highlighted where the leak has occurred in the method of a .m file in the project.

 Hope this helps to someone new in fixing memory leaks !

Monday, April 11, 2011

Tutorial on UIGestureRecognizer

I am going to cover Gestures topic today.

We have 2 views on our ViewController , they toggle on the swipe gesture of the user.

You will need to define a view class with the gesture added to it as following

You will have to define a protocol in its header file to be able to talk to main controller class which we will implement below in the blog.

This is another view with another swipe gesture implemented on it.
Same as above class we will to create protocol in its header file as well to be able to talk to main view controller.

Now we have a view controller class, where above views are added as sub view.

Firstly , there is view 1 visible and view 2 is hidden.
User swipes to left, it will hide view 1 and  display view 2.
User swipes to right, it will hide view 2 again and display view 1.

You can the protocol methods implemented in this view controller class.

You will have to include the class1 and class2 protocol in your view controller's header file as following:

You can download the code, to study the code more deeply.

Download Sample Code

How to get raw data from the Image ?

Now you can have all the pixel data in rawData. You can access it as following:

where offset goes from 0 to 4*w*h with an increment of 4.

Saturday, April 9, 2011

Recognize single and double tap on iPhone using UITapGestureRecognizer

If you have used elements like UIButton, UIImageView and you want to find out whether user tapped once or twice on it then you can do following and solve the problem very easily !

1. Create a subclass of UIButton or UIImageView whatever it is, in its implementation class you can define UITapGestureRecognizer methods like following:

Then one of the following methods will be executed whenever a touch event is generated by user.You need to write these methods in the same file as above .

And your work is finished here now. You can just create an object of the class you have defined above and enjoy the single tap / double tap work like charm ... you dont need to code the hell out to recognise the single or double tap now !!

Tuesday, April 5, 2011

Difference between NSString: isEqualTo and NSString: compare

isEqualTo is used for comparing the value of string.

for e.g.

NSString *a=@"Reetu";
NSString *b=@"Mike";
NSString *c=@"Mike";

if([a isEqualTo:b]) -> It will return No;
if([c isEqualTo:b]) -> It will return YES;

compare is used comparing(generally for sorting the string) by lexical order(in dictionary which string will come first).

if([a compare:b]) -> It will return 1;
if([b compare:a]) -> It will return -1;

Where is Application Loader with XCode 4 ?

See yourself in screenshow below