0

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

user1516825
  • 133
  • 1
  • 1
  • 6
  • post some code which creates uiimagepicker and json stuff – Apurv Jul 11 '12 at 08:53
  • improper use of memory?please post some codes – DLende Jul 11 '12 at 08:53
  • 2
    My car runs about 1 mile and stops. Whats the reason behind this? Please rephrase your question, add more details, otherwise nobody will help you. – yan.kun Jul 11 '12 at 08:53
  • Get yourself familiar with memory management and proper reactions to the memory warning. Your view controllers have a didReceiveMemoryWarning methodl. Overwrite it and free up any memory that is not currently required. You will have to release some memory in response to that message. If you continue allocating memory instead, your app will finaly be terminated by iOS. – Hermann Klecker Jul 11 '12 at 08:56
  • use memory leaks tool to find your memory issues. – Leena Jul 11 '12 at 09:03

2 Answers2

4

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];
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
sschunara
  • 2,285
  • 22
  • 31
  • write [imgPicker release] at end of viewDidLoad method after [self presentModalViewController:imgPicker animated:YES]; this line. – sschunara Jul 11 '12 at 09:19
0

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
Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69