Things I Learnt Today

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Mac Ports:

An awesome initiation by the Open-source community to provide a single application, called as Ports, which keeps track of most of the open-source projects thus reducing

  • the hassle of manual compilation of your favorite open source project
  • manual tracking of the latest version of your favorite open source project

All you have to say is the open source project name / software title , a.k.a port, you are interested, and Port utility will take care of

  • Finding the Latest stable version for the latest OS Version
  • Downloading the appropriate source
  • compiling and creating an executable

With Mac Ports available it is very easy to compile your JPEG library, or for that matter installing python, which is as easy as saying

sudo ports – v python +universal

This is the tool which help the Novice developers / or the ones who are very much interested in getting their Favorite open source project up and running rather than indulging in dirt of compilation

For more information on Mac Ports Visit the community site here

 

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Python Imaging Library(PIL)

Did a Hands On on setting up Python Imaging Library and there came the use of ports where I had to Install JPEG, XML, FreeType libraries. This is wonderful library which supports a various Image Processing Options, with Image Comparison is the one which I was interested upon.

Installation of PIL is explained step by step here, I was installing this on Snow Leopard and was with this issue

“_imaging.so module not installed”

OR

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Python/2.6/site-packages/PIL/_imaging.so, 2): 
Symbol not found: _jpeg_resync_to_restart
  Referenced from: /Library/Python/2.6/site-packages/PIL/_imaging.so
  Expected in: flat namespace
 in /Library/Python/2.6/site-packages/PIL/_imaging.so

If Ypur not so lucky enough to get the PIL installed due to this Error, then you have come to the right place. This issue is because the Python in Snow Leapard is looking for 64 bit JPEG library by default.

I have followed the steps nailed here and it worked for me

Hope this helps.

iOS Open Folder Animation

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

As I was fiddling with my iPad,  Open Folder Animation in iOS caught my attention. After giving it a serious though on how to achieve it, It looked Trivial.

iOS Folder View

 

Let us get together to write a piece of code which does this cool animation.

 

1. Plan

iOS_Folder_View_Plan.png

We can achieve this using 3 Main Layers

1. Layer -1 (Main View) : The Lowest layer contains the main screen which stores the main content

2. Layer -2  (Folder View): The Folder View, which hosts the Folder Icons. Once the Folder Opens, This is the only view which is active accepting the Touch Event. This layer resides above Layer-1

3. Layer-2.1 (Arrow ) : The Arrow, which points to the selected folder. This is movable on x-Axis.

4. Layer-3 (Layer to host Bottom part of Main Content View) : This layer hosts the bottom part of the Main Content View (Layer -1) i.e from point where Folder View ( Layer-2) occupies to the end of the Main Content View. This Layer is place above the Folder View (Layer-2 ) in terms of Level. In Terms of position on screen, this layer is placed just below the Folder View(Layer-2)

 

2. Execution

We can achieve Open Folder Animation effect in 3 Simple Steps

STEP 1: Capture the Main View Content into an image

  • Capturing Main View Content Image can be achieve using renderInContext: API. as shown in code snippet below
  • Clip the Main View into two parts, One part is the visible portion above the Folder view and rest of the image would be hosted in Layer -3. The Trick used in sample code below to avoid the cutting of the image into two parts, is by setting the Layer-3 bounds, such that it should hide the top portion (Part-1) of the Main Content view.

-(void)captureImageFromPointAndSetupMaskView:(CGPoint)selectedFolderPoint

{

UIGraphicsBeginImageContext(mMainBackgroundView.frame.size);

//capture the main content view

[mMainBackgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

[mBottomPartOfMainBackgroundView setImage:backgroundImage];

//set the bounds such that the we hide the part of the main content image, which is visible just above the Folder View.

//This gets the continuation effect on the image once the folder view appears on screen

[mBottomPartOfMainBackgroundView.superview setBounds:CGRectMake(0.0, selectedFolderPoint.y + mSelectedArrowTipView.frame.size.height, mMainBackgroundView.frame.size.width, mMainBackgroundView.frame.size.height)];


}

 

STEP 2: Layout the Folder View(Layer 2 & 2.1) and Bottom Part Of Main Content View (Layer -3)

  • Folder View and Arrow View are grouped in Single View.
  • Place The Arrow View such that it is right below and to the Center of the Tapped Folder Icon Tapped
  • Place the Folder View Just below Arrow View

-(void)layoutBottomPartOfMainViewRelativeToPointInMainView:(CGPoint)selectedFolderPoint

{

//Place the Folder View Just below Arrow View

CGRect folderViewFrame = [mFolderView frame];

folderViewFrame.origin.y = floorf(selectedFolderPoint.y);

[mFolderView setFrame:folderViewFrame];

//making sure the View which displays bottom part of Main Backgorund View is just below  the Folder View

CGRect maskFrame = mBottomPartOfMainBackgroundView.superview.frame;

maskFrame.origin.y = folderViewFrame.origin.y + mSelectedArrowTipView.frame.size.height;

mBottomPartOfMainBackgroundView.superview.frame = maskFrame;

//Place The Arrow View such that it is right below and to the Center of the Tapped Folder Icon Tapped

[UIView setAnimationsEnabled:NO];

mSelectedArrowTipView.center = CGPointMake(selectedFolderPoint.x, 0.0);

CGRect arrowFrame = mSelectedArrowTipView.frame;

arrowFrame.origin.y = 0.0;

mSelectedArrowTipView.frame = arrowFrame;

 

[UIView setAnimationsEnabled:YES];

}


 

STEP 3- Push The Layer-3 which hosts the other part of the Main Content View

-(void)layoutFinalFrameOfBottomPartOfMainContentView

{

//Push The Layer-3 which hosts the other part of the Main Content View

CGRect maskFrame = mBottomPartOfMainBackgroundView.superview.frame;

maskFrame.origin.y = mFolderView.frame.origin.y + mFolderView.frame.size.height;

mBottomPartOfMainBackgroundView.superview.frame = maskFrame;

}

 

Thats it !! The above 3 steps would be called upon tapping a Folder  Icon as show in below code snippet

-(IBAction)openFolder:(id)sender

{

//get the center point of the tapped folder

CGPoint selectedFolderPoint = CGPointMake([sender center].x, [sender frame].origin.y + [sender frame].size.height);

if( mFolderView.hidden )//if folder is not opened

{

//open folder animation

//STEP 1: Capture the Main View Content into an image

[self captureImageFromPointAndSetupMaskView:selectedFolderPoint];

//STEP 2: Layout the Folder View(Layer 2 & 2.1) and Bottom Part Of Main Content View (Layer -3)

[self layoutBottomPartOfMainViewRelativeToPointInMainView:selectedFolderPoint];

[UIView beginAnimations:@”FolderOpen” context:NULL];

[UIView setAnimationDuration:0.5];

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

//captures the main background view’s image

mFolderView.hidden=NO;

mBottomPartOfMainBackgroundView.superview.hidden=NO;

//STEP 3- Push The Layer-3 which hosts the other part of the Main Content View

[self layoutFinalFrameOfBottomPartOfMainContentView];

[UIView commitAnimations];

}

else {

//close folder animation

[UIView beginAnimations:@”FolderClose” context:NULL];

[UIView setAnimationDuration:0.5];

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

[UIView setAnimationDidStopSelector:@selector(animation:didFinish:context:)];

[UIView setAnimationDelegate:self];

//restore the layout and hide the folder view after animation

[self closeFolder:selectedFolderPoint];

[UIView commitAnimations];

}

}

 

3. Result

Download the Full Source Code Here : iOS Open Folder Animation.zip

A Video of the Sample Code can be found here: iOS Open Folder Animation Video

[youtube=http://www.youtube.com/watch?v=xBB1humerBw]

If you like this post, Please leave your feedback / comment and Share this (See below for sharing options) to spread to all others who might be interested 🙂

Happy Coding 🙂

How to compare objects of your custom classes?

[This post is intended only for audiences with Basic knowledge in Objective-C]

With Cocoa / UIKit framework has built-in support for comparing objects like NSDate, NSString, NSData, NSNumber and few others which would help us to identify whether two objects are EQUAL, GREATER THAN or LESSER THAN the other object. This in-turn is used in cases like sorting, searching.

 

We would like to see how the same can be achieved for the custom classes we write, where we can specify the Equality condition by our own.

By default, NSObject class allows us to determine equality between two objects whose class is derived from NSObject through

isEqual: method,

where Apple documentation defines it as

This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YES to an isEqual: request. See the NSArray, NSDictionary, NSString and NSData class specifications for examples of the use of this method.

If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.

 

 

In short, At NSObject (root class) level, two objects are said be equal

  • if their hash value are same
  • two objects have same address.

Well this may not be the case when we write our custom class,  for example,  two NSString objects might have different  pointer addresses, but have same string value. Hence, one must override hash method and isEqual: method of NSObject. NSString class would have hash and isEqual defined as below

– (NSUInteger)hash

{

//return the hash value generated from the string.

}

-(BOOL)isEqual:(id)otherObject

{

// return strcmp ( [self’s cString] , [otherObject’s cString] )

}

 

Where do I apply this?

Well, if you encounter a situation where you have an array (or in general collection) of Employees for eg, and you would want to remove an employee of particular ID, then you can do it with a single call like this. This would save you from writing an search algorithm.

Employee *employeeToRemove;

[myEmployees removeObject: employeeToRemove]; // assuming myEmployees is of type NSMutableArray

As Apple document says, removeObject and its variants (see below list for variants) would pick the object to remove for which isEqual: would return TRUE

  • – removeObject:
  • – removeObjectIdenticalTo:
  • – removeObjectIdenticalTo:inRange:
  • – removeObjectsInArray:
  • – indexOfObject:
  • – indexOfObject:inRange:
  • – indexOfObjectIdenticalTo:
  • – indexOfObjectIdenticalTo:inRange:

 

Another method that does fall in this category, is compare: method (actually you can give a custom name to this method ) whose signature is

-(NSComparisonResult)compare:(id)otherObject

NSComparisonResult is an enum with values  as below

enum {

NSOrderedAscending = -1,

NSOrderedSame,

NSOrderedDescending

};

 

With this method implemented in your Custom class (for example, Employee class), you can sort your objects in a collection (NSMutableArray , NSArray) with a single statement

[ myEmployee sortArrayUsingSelector(compare:)];

Other variants where this strategy can be used are

– sortedArrayUsingSelector:

– sortUsingSelector:

 

Hope this tutorial find useful for you.

Happy Coding 🙂

 

Building Objective-C static libraries with categories

I just came across the usage of Linker flag -all_load and -ObjC

Basically, this is used in situations where you build a static library with Categories. When this static library is linked with the actual implemenation / application that use this library, -ObjC linker flag would inform the compiler to load all the Objective C classes implementation.

However, since there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories, we should use -all_load or -force_Load Linker flags.

For More Details. See Apple Technical QA QA1490

Tools helpful in Creating Universal iOS Apps

This post is about Tools [Code Snippets] which I prepared in the process of creating Universal iOS Apps

 

1. Identifying the Device

 

#device DEVICE_IS_IPAD (UIUserInterfaceIdiomiPad == [BBUtilities getUserInterfaceIdiom])

#device DEVICE_IS_IPHONE (UIUserInterfaceIdiomiPhone == [BBUtilities getUserInterfaceIdiom])

 

/*snippet to get user interface idiom*/

static UIUserInterfaceIdiom sDeviceUIIdiom = -1; -(UIUserInterfaceIdiom)getUserInterfaceIdiom

{

 

//We make sure that this method is invoked only once throughout the application lifetime

if( sDeviceUIIdiom == -1)

{

if([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)])

{

sDeviceUIIdiom = [[UIDevice currentDevice] userInterfaceIdiom];

}

}

return sDeviceUIIdiom;

}

 

2. Identifying iPhone 4 Device

 

#define DEVIS_IS_IPHONE4 ([BBUtilities isiPhone4])

 

/*snippet to get whether current device is iPhone 4 or not*/

static UIUserInterfaceIdiom sIsiPhone4 = -1;

 

-(BOOL) isiPhone4

{

//We make sure that this method is invoked only once throughout the application lifetime     i

if( sIsiPhone4 == -1)

{

if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])

{

sIsiPhone4 = [[UIScreen mainScreen] scale] ==2.0 ;

}

else

{

sIsiPhone4=0;

}

}

return sIsiPhone4;

}

Quick Tip : Initializing static variables of a class in Objective C

Initializes the receiver before it’s used (before it receives its first message).

 

+ (void)initialize


Discussion The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.


[Reference : NSObject Class Reference]

You can place all your static member variable initialization code in this method as it guarantees it would be called only once BEFORE any method in that Class is invoked through any of its objects.

Happy Coding 🙂

Image Size With Aspect Ratio

A Snippet to calculate an Image Size to fit within a given rectangle with its Aspect Ratio maintained

ImageSize NewSizeForImageOfSize(ImageSize srcImageSize, ImageSize destSize)
{

ImageSize newSize;

if( srcImageSize.width > srcImageSize.height)
{
newSize.width=destSize.width;     newSize.height= srcImageSize.height * (destSize.width/srcImageSize.width)
}
else
{
newSize.height=destSize.height;     newSize.width= srcImageSize.width * (destSize.height/srcImageSize.height)
}

return newSize;
}

 


Search 4 Twitter xAuth and OAuth Samples ENDS here

Hello,

This post is place where you will be re-directed to places where you learn about xAuth and OAuth w.r.t Twitter, Take away Sample code of Twitter Engine supporting xAuth and OAuth both for i[Phone] OS and Mac OS Platforms

  1. Official Twitter API Documentation
  2. xAuth Explained
  3. xAuth for Browserless Token Exchange
  4. OS X 10.5 Demo With Sample Code [See References section in that site]
  5. iPhone xAuth Explained with Sample Code
  6. MGTwitterEngine xAuth Support
  7. Testing TwitPIC OAUTH

 

 

Others

  • iPhone Dev SDK – Twitter Agent
  • SlideShow Presentation
  •  

    Hope this Helps!!

    Have Fun 🙂