user1516825
user1516825

Reputation: 133

Memory Warning in objective c

In my application while i'm call json or open uiimagePicker camera source 4-5 time continuesly i'm getting Received memory warning. Level=1 and immediately my application getting crash. What the reason behind this

i'm using custom UIImagePickerController eg. code below....

- (void)viewDidLoad {
[super viewDidLoad];


[[UIApplication sharedApplication] setStatusBarHidden:YES];
front = YES;
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.delegate = self;
imgPicker.showsCameraControls = NO;

cameraOverlayView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cam-land03.png"]];
cameraOverlayView.alpha = 0.0f;
imgPicker.cameraOverlayView = cameraOverlayView;

// animate the fade in after the shutter opens
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.2f];
cameraOverlayView.alpha = 1.0f;
[UIView commitAnimations];
[cameraOverlayView release];

toolBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 425, 320, 55)];
toolBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-bar.png"]];
toolBarView.layer.borderColor = [[UIColor whiteColor] CGColor];
toolBarView.layer.borderWidth = 0.0f;

[imgPicker.view addSubview:toolBarView];

cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cameraBtn.frame = CGRectMake(110,5,100,47);
[cameraBtn setImage:[UIImage imageNamed:@"land-top.png"] forState:UIControlStateNormal];
[cameraBtn addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:cameraBtn];

cancel = [UIButton buttonWithType:UIButtonTypeCustom];
cancel.frame = CGRectMake(10,13,50,30);
[cancel setImage:[UIImage imageNamed:@"btn-cancel.png"] forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[toolBarView addSubview:cancel];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didOrientation:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

[self presentModalViewController:imgPicker animated:YES];

}

and While i call sync response from the server it will take 30-40 seconds to receive response and store local db.

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@"url"];                                                          

    NSUserDefaults *authCode = [NSUserDefaults standardUserDefaults];
NSString *auth = [authCode stringForKey:@"authcode"];

NSUserDefaults *profileId = [NSUserDefaults standardUserDefaults];
NSString *profile = [profileId stringForKey:@"profileId"];

purchaseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/attachbusinesscard",myString]];

NSData *data = [[NSData alloc] init];
data = UIImagePNGRepresentation(image);

NSUInteger length = [data length];
unsigned char *bytes = malloc( length * sizeof(unsigned char) );

// Get the data
[data getBytes:bytes length:length];
NSLog(@"AfterByteConvertion");

NSMutableString *strCrop = [[NSMutableString alloc] init];
for (int i=0; i<[data length]; i++) {
[strCrop appendString:[NSString stringWithFormat:@"%d",bytes[i]]];
    if (i!=[data length]-1) {
    [strCrop appendString:@","];
    }
 }

 NSString *postString;

 if (forBack) {
    postString= [NSString stringWithFormat:@"{\"AuthCode\":\"%@\",\"ProfileId\":\"%@\",\"Back\":[%@],\"Front\":\"\",\"Id\":\"%@\"}",auth,profile,strCrop,str];  
    forBack=NO;
 }
  else {
    postString= [NSString stringWithFormat:@"{\"AuthCode\":\"%@\",\"ProfileId\":\"%@\",\"Back\":\"\",\"Front\":[%@],\"Id\":\"%@\"}",auth,profile,strCrop,str];
 }  

 NSData *requestData = [NSData dataWithBytes:[postString    UTF8String] length:[postString length]];
 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:purchaseURL];
 [request setHTTPMethod:@"POST"];

 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

 [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

 [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];

 [request setHTTPBody:requestData];//[postString dataUsingEncoding:NSUTF8StringEncoding]];

  NSURLConnection *purchaseConn =[[NSURLConnection alloc]
                                    initWithRequest:request
                                    delegate:self];
      if (purchaseConn) {
    webData = [[NSMutableData data] retain];
  }

Please help me

Upvotes: 0

Views: 1955

Answers (2)

sschunara
sschunara

Reputation: 2285

If you are taking UIImagePickerController object globally then release the UIImagePickerController object in

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 ....
 .....

  [imgPicker release];

}

if you are taking UIImagePickerController object locally release

[self.navigationController presentModalViewController:imgPicker animated:YES];
[imgPicker release];

Upvotes: 4

Praveen S
Praveen S

Reputation: 10395

When you get memory warnings you need to free the memory you no longer want to use.

  1. You have to implement didReceiveMemoryWarning
  2. You can also consider moving to ARC as a part of optimisation

Upvotes: 0

Related Questions