Sunday, December 16, 2012

How to find a KEY from NSDictionary?

Here we will take NSMutableArray with objects and NSDictionary with Objects and Keys.



    NSMutableArray* mArray = [[NSMutableArray alloc]
                              initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    
    NSMutableDictionary *mDic = [NSMutableDictionary
                                 dictionaryWithObjectsAndKeys:@"F",@"mF",@"G",@"mG",@"B",@"mB",@"I",@"mI",nil];



We will write a Function which can provide us the array of founded keys after comparison.


-(NSMutableArray*)compareMyObject:(NSMutableArray*)array:(NSDictionary*)dic
{
    NSMutableArray* keysArray = [[NSMutableArray alloc] init];
for(int k=0;k<[array count];k++)
{
NSString* mStr = [array objectAtIndex:k];
        NSArray* array = [dic allKeysForObject:mStr];
if([array count])
        {
            [keysArray addObject:[array objectAtIndex:0]];
        }
}
    return keysArray;
}

Now we will call this method in the following format.

    NSMutableArray* keysArray =  [self compareMyObject:mArray:mDic];

The above method gives us the result array now, We will print and check those results.

    NSLog(@"total %d keys found. Those are %@",[keysArray count],[keysArray description]);


The result is : 

keysArray = (
    mB,
    mF




Wednesday, February 1, 2012

How to capture the screen with frame specific?

    CGRect mRect = CGRectMake(0,0,730,905);
    UIGraphicsBeginImageContext(mRect.size);
    [self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

mRect is any frame which you wanted to take screen shot and containerview is the view where we need to take the screenshot.

A small examples with Animations

In iOS Sdk there are two types of animation which is used frequently.

with CoreAnimation

            CATransition *animation = [CATransition animation];
            [animation setDuration:1.5];
            [animation setType:kCATransitionMoveIn];
            [animation setSubtype:kCATransitionFromRight];
            [animation setSpeed:1.5];
            [[imageView layer] addAnimation:animation forKey:nil];

Here imageView is the view where you need to apply the animation. the view with come from right to left with the specific duration.

setDuration - to set the animation happen time
setType - to set the animation type

see CATransition class to get more details.

with UIAnimation

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:2.0];
        [UIView setAnimationDelay:1.0];
        [UIView setAnimationDelegate:self];
        myImageView.alpha = 0.0;
        [UIView commitAnimations];

 Here i wanted to hide the myImageview. Instead of hiding with
    myImageView.hidden = YES;
I thought of adding some animation to hide the view.
It will hide the view in 2 sec after 1 sec of delay.


We have delegate methods to use more effectively. Just have a look at the Class.






How to print the Frame(CGRect) of a View?

    NSLog(@"self.view.Frame=%@", NSStringFromCGRect(self.view.frame));

Here self.view is the view of UIView Class. You can print any frame with this log.