Monday, 16 March 2015

ios memory management

Memory management in iOS was initially non ARC(Automatic reference counting) where we have to retain and release the objects. Now it supports ARC where we don't need to add retain and release. Actually the Xcode takes care of the job automatically in compile time.
Problems faced

The two major problems faced as per apple documentation are,

    Freeing or overwriting data that is still in use. This causes memory corruption, and typically results in your application crashing, or worse, corrupted user data.
    Not freeing data that is no longer in use causes memory leaks. A memory leak is where allocated memory is not freed, even though it is never used again. Leaks cause your application to use ever-increasing amounts of memory, which in turn may result in poor system performance or (in iOS) your application being terminated.

Memory Management rules

    We own the objects we create and we have to subsequently release them when they are no longer needed.
    Retain can be used t gain ownership of an object that we did not create. We have release these objects too when it's not needed.
    Don't release the objects that we don't own.

Handling memory in ARC

You don't need to use release and retain in ARC. So, all the view controller's objects will be released when the view controller is removed. Similarly any objects sub objects will be released when they are released. Remember if other classes have strong reference to an object of the class then the whole class won't be released. So it is recommended to use weak properties for delegates.
Using memory management tools

We can analyze the usage of memory with the help of Xcode tool Instruments. It includes tools like activity monitor, Allocations, leaks, zombies and so on.

Ios Camera management

Introduction

Camera is one of the common features of mobile devices. It is possible for us to take pictures with the camera and use it in our application and it is quite simple too.

Steps Involved

1. Create a simple View based application.

2. Add a button in ViewController.xib and create IBAction for the button.

3. Add an image view and create IBOutlet naming it as imageView.

4. Update ViewController.h as follows.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate>
{  
   UIImagePickerController *imagePicker;
   IBOutlet UIImageView *imageView;   
}
- (IBAction)showCamera:(id)sender;

@end

5. Update ViewController.m as follows.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showCamera:(id)sender {
    imagePicker.allowsEditing = YES;
    if ([UIImagePickerController isSourceTypeAvailable:
        UIImagePickerControllerSourceTypeCamera])
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else{
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentModalViewController:imagePicker animated:YES];

}
-(void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image == nil) {   
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    imageView.image = image;
   
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissModalViewControllerAnimated:YES];
}

@end