【问题标题】:Memory Warning in objective c目标 c 中的内存警告
【发布时间】:2012-07-10 20:22:17
【问题描述】:

在我的应用程序中,当我连续 4-5 次调用 json 或打开 uiimagePicker 相机源时,我收到 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];
  }

请帮帮我

【问题讨论】:

  • 发布一些创建 uiimagepicker 和 json 东西的代码
  • 内存使用不当?请贴一些代码
  • 我的车跑了大约 1 英里然后停了下来。这背后的原因是什么?请改写您的问题,添加更多详细信息,否则没有人会帮助您。
  • 让自己熟悉内存管理和对内存警告的正确反应。您的视图控制器有一个 didReceiveMemoryWarning 方法。覆盖它并释放当前不需要的任何内存。您将不得不释放一些内存来响应该消息。如果你继续分配内存,你的应用最终会被 iOS 终止。
  • 使用内存泄漏工具查找内存问题。

标签: iphone objective-c memory-management


【解决方案1】:

如果您在全局范围内使用UIImagePickerController 对象,则在

中释放UIImagePickerController 对象
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 ....
 .....

  [imgPicker release];

}

如果你是把UIImagePickerController对象本地释放

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

【讨论】:

  • 在 [self presentModalViewController:imgPicker animated:YES] 之后的 viewDidLoad 方法末尾写入 [imgPicker release];这一行。
【解决方案2】:

当您收到内存警告时,您需要释放不再想使用的内存。

  1. 你必须实现didReceiveMemoryWarning
  2. 您还可以考虑迁移到 ARC 作为优化的一部分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多