Iphone 目标c中的记忆警告

Iphone 目标c中的记忆警告,iphone,objective-c,memory-management,Iphone,Objective C,Memory Management,在我的应用程序中,当我连续4-5次调用json或打开uiimagePicker相机源代码时,我收到收到内存警告。级别=1,我的应用程序立即崩溃。这背后的原因是什么 我正在使用自定义UIImagePickerController,例如下面的代码 - (void)viewDidLoad { [super viewDidLoad]; [[UIApplication sharedApplication] setStatusBarHidden:YES]; front = YES; imgPicker

在我的应用程序中,当我连续4-5次调用json或打开uiimagePicker相机源代码时,我收到
收到内存警告。级别=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];

}
当我从服务器调用sync response时,接收响应和存储本地数据库需要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];
  }
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];
数据=UIImagePngResentation(图像);
NSU整数长度=[数据长度];
无符号字符*bytes=malloc(长度*sizeof(无符号字符));
//获取数据
[data getBytes:字节长度:长度];
NSLog(@“后字节转换”);
NSMutableString*strCrop=[[NSMutableString alloc]init];

对于(int i=0;i当您收到内存警告时,您需要释放不想再使用的内存

  • 你必须实施
  • <> LI>您也可以考虑将ARC作为优化的一部分
    当收到内存警告时,您需要释放不想再使用的内存

  • 你必须实施
  • <> LI>您也可以考虑将ARC作为优化的一部分
    如果要全局获取
    UIImagePickerController
    对象,请在中释放
    UIImagePickerController
    对象

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
     ....
     .....
    
      [imgPicker release];
    
    }
    
    如果您正在本地拍摄
    UIImagePickerController
    对象,请释放

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

    如果要全局获取
    UIImagePickerController
    对象,请在中释放
    UIImagePickerController
    对象

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
     ....
     .....
    
      [imgPicker release];
    
    }
    
    如果您正在本地拍摄
    UIImagePickerController
    对象,请释放

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

    发布一些创建uiimagepicker和json的代码内存使用不当?请发布一些代码我的车行驶了大约1英里并停了下来。这背后的原因是什么?请重新表述您的问题,添加更多细节,否则没有人会帮助您。熟悉内存管理和对内存警告的正确反应。您的视图控制器有didReceiveMemoryWarning方法。覆盖它并释放当前不需要的内存。您必须释放一些内存以响应该消息。如果继续分配内存,您的应用程序最终将被iOS终止。使用内存泄漏工具查找内存问题。发布一些创建ui的代码imagepicker和json Stuff内存使用不当?请发布一些代码我的车行驶了大约1英里并停了下来。这背后的原因是什么?请重新表述您的问题,添加更多详细信息,否则没有人会帮助您。熟悉内存管理和对内存警告的正确反应。您的视图控制器有didReceiveMemoryWarning METHOL。覆盖它并释放当前不需要的内存。您必须释放一些内存以响应该消息。如果继续分配内存,您的应用程序最终将被iOS终止。使用内存泄漏工具查找内存问题。在viewDidLoad方法结束后写入[imgPicker release][self-presentModalViewController:imgPicker动画:是];此行。在视图末尾写入[imgPicker release]在[self-presentModalViewController:imgPicker动画:是]之后加载方法;此行。