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 回答