0

私のアプリケーションでは、jsonに電話をかけている間、またはuiimagePickerカメラソースを4〜5回開いているときに、継続的に取得Received memory warning. Level=1し、すぐにアプリケーションがクラッシュします。この背後にある理由は何ですか

私はカスタムUIImagePickerControllerを使用しています。以下のコード...

- (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];

}

サーバーから同期応答を呼び出している間、応答を受信して​​ローカルデータベースを保存するのに30〜40秒かかります。

    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];
  }

私を助けてください

4

2 に答える 2

4

UIImagePickerControllerオブジェクトをグローバルに取得している場合は、UIImagePickerControllerオブジェクトを解放します

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

  [imgPicker release];

}

UIImagePickerControllerオブジェクトをローカルにリリースする場合

[self.navigationController presentModalViewController:imgPicker animated:YES];
[imgPicker release];
于 2012-07-11T09:00:45.813 に答える
0

メモリの警告が表示されたら、不要になったメモリを解放する必要があります。

  1. didReceiveMemoryWarningを実装する必要があります
  2. 最適化の一環として ARC への移行を検討することもできます
于 2012-07-11T08:54:34.903 に答える