Tips and Tricks – Part 3

In this article i am going to explain some of the programming solutions

1. Capture device screenshot programmatically

    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

Here, in above code we first pass the rect which we want to capture as image.Then we pass this rect
to beginContext method which take three parameters

rect of which we need screenshot
Captured image to be opaque or not
Image to be scaled

Then we started our CGContext and then we capture from current context.

2. Insert background image to toolBar

[toolBar insertSubview:[[[UIImageView alloc]
                                 initWithImage:[UIImage imageNamed:@”BottomBar_iPad.png”]] autorelease] atIndex:1];

Here in above code we use insertSubview method of toolbar.

3. Show splash screen for certain interval of time

    [NSThread sleepForTimeInterval:3.0];

Here in above line we set main thread to sleep for 3 seconds. You can set value as per your need.

4. Send object with a selector

We can pass objects while calling our selector in objective C by wrapping object in withObject parameter. If you want to pass multiple objects then we can add our objects in an array and then pass that array to withObject parameter.

 NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
    [array addObject:@”http://i62.tinypic.com/13yjcqh.png”];   
    [array addObject:personImageView];
    [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:array];

#pragma mark Load image

-(void)loadImage:(id)sender
{
    NSMutableArray *array =(NSMutableArray *)sender;
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[array objectAtIndex:0]]];
    UIImageView *img = [array objectAtIndex:1];
    img.image=[UIImage imageWithData:data];
    data = nil;
}

5. Compare two images

You can compare two images using below code

 UIImage *image = [UIImage imageNamed:@”abc.png”];
 UIImage *imageTwo = [UIImage imageNamed:@”abc2.png”];
 
        if ([image isEqual:imageTwo])
        {
            NSLog(@”BOTH IMAGES ARE SAME”);
        }
        else
        {
             NSLog(@”BOTH IMAGES ARE DIFFERENT”);
        }

6. Get image name from UIImageView

To get image name from UIImageView, first you have to set image name to setAccessibilityIdentifier  property of UIImage.

 [myImageview.image setAccessibilityIdentifier:@”abc.png”];

To get name of image from myImageview use the below code

NSLog(@”Image name of image assigned to myImageview is %@”,[myImageview.image accessibilityIdentifier]);

7. Allow your app awake until user press home button or close app

You can use below line of code to let your app awake until user press home button or close your app

 [[UIApplication sharedApplication]setIdleTimerDisabled:YES];

8. Restrict device to receive touch interaction

In some cases, we need our app to ignore user touch events until a particular  operation going on gets finished. You can do this by using below code

[[UIApplication sharedApplication]beginIgnoringInteractionEvents];

And to resume receiving user interaction events

 [[UIApplication sharedApplication]endIgnoringInteractionEvents];

9. Load view from an xib

To load a view from an xib, you can use below code

UIView *notificationView = [[[NSBundle mainBundle] loadNibNamed:@”NotificationManager” owner:self options:nil] objectAtIndex:0];

Above code will assign .xib file as view. So no need to create a view programmatically.Simply create an xib file and assign it to view.