收到内存警告。等级= 2,程序接收信号:“0”

时间:2011-01-10 11:50:06

标签: iphone ios4

嗨朋友我从webservice获取图像并在表视图中加载这些图像。但是当我继续滚动程序时,接收内存警告level1,level2然后app退出状态为0.这只发生在设备中而不是模拟器中。 这是我的代码,请帮助我。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -4, 62, 30)];
    [infoButton setBackgroundImage:[UIImage imageNamed: @"back.png"] forState:UIControlStateNormal];
    [infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    self.navigationItem.leftBarButtonItem = customBarButtomItem;
    [customBarButtomItem release];
    [infoButton release];

    UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 62, 30)];
    [homeButton setBackgroundImage:[UIImage imageNamed: @"home.png"] forState:UIControlStateNormal];
    [homeButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
    //[homeButton setTitle:@"UPLOAD" forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(homeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtom = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
    self.navigationItem.rightBarButtonItem = rightBarButtom;
    [rightBarButtom release];
    [homeButton release];

    sellerTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 416) style:UITableViewStylePlain];
    sellerTableView.delegate=self;
    sellerTableView.dataSource=self;
    sellerTableView.backgroundColor=[UIColor clearColor];
    sellerTableView.scrollEnabled=YES;
    //table.separatorColor=[UIColor grayColor];
    //table.separatorColor=[UIColor whiteColor];
    //[[table layer]setRoundingMode:NSNumberFormatterRoundDown];
    [[sellerTableView layer]setBorderColor:[[UIColor darkGrayColor]CGColor]];
    [[sellerTableView layer]setBorderWidth:2];
    //[[productTable layer]setCornerRadius:10.3F];
    [self.view addSubview:sellerTableView];

    appDel = (SnapItAppAppDelegate*)[[UIApplication sharedApplication] delegate];



}

-(void)viewWillAppear:(BOOL)animated
{


    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center = self.view.center;
    [self.view addSubview:spinner];
    [spinner startAnimating];
    [[SnapItParsing sharedInstance]assignSender:self];
    [[SnapItParsing sharedInstance]startParsingForShowProducts:appDel.userIdString];
    [sellerTableView reloadData];

}


-(void)showProducts:(NSMutableArray*)proArray
{ 

    if (spinner) {
        [spinner stopAnimating];
        [spinner removeFromSuperview];
        [spinner release];
        spinner = nil;
    }


    if ([[[proArray objectAtIndex:1]objectForKey:@"Success"]isEqualToString:@"True"]) {
        //[self.navigationController popViewControllerAnimated:YES];
        //self.view.alpha=.12;

        if (productInfoArray) {
            [productInfoArray release];
        }
        productInfoArray=[[NSMutableArray alloc]init];

        for (int i=2; i<[proArray count]; i++) 
        {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            [productInfoArray addObject:[proArray objectAtIndex:i]];
            NSLog(@"data fetch array is====> /n%@",productInfoArray);
            [pool release];
        }
    }
}

#pragma mark (tableview methods)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //return [resultarray count]; 

    return [productInfoArray count];
}


-(void)loadImagesInBackground:(NSNumber *)index{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableDictionary *frame = [[productInfoArray objectAtIndex:[index intValue]] retain];
    //NSLog(@"frame value ==>%@",[[frame objectForKey:@"Image"]length]);
    NSString *frameImagePath = [NSString stringWithFormat:@"http://apple.com/snapit/products/%@",[frame objectForKey:@"Image"]];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:frameImagePath]];
    NSLog(@"FRAME IMAGE%d",[[frame valueForKey:@"Image" ]length]);

    if([[frame valueForKey:@"Image"] length] == 0){
        NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"no_image.png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [frame setObject:image forKey:@"friendImage"];
        [imagePath release];
        //[image release];
    }
    else {
        //NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:frameImagePath]];
        NSLog(@"image data length ==>%d",[imageData length]);
        if([imageData length] == 0){
            NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"no_image.png"];
            UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
            [frame setObject:image forKey:@"friendImage"];
            [imagePath release];
            //[image release];
        }
        else {
            //UIImage *image = [[UIImage alloc] initWithData:imageData];
            UIImage *image = [UIImage imageWithData:imageData];
            [frame setObject:image forKey:@"friendImage"]; 
            //[image release];
        }
    }
    [frame release];
    frame = nil;

    [self performSelectorOnMainThread:@selector(reloadTable:) withObject:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[index intValue] inSection:0]] waitUntilDone:NO];

    [pool release];

}

-(void)reloadTable:(NSArray *)array{
    NSLog(@"array ==>%@",array);
    [sellerTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *celltype=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celltype];


    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor=[UIColor clearColor];
        //cell.textLabel.text=[[resultarray objectAtIndex:indexPath.row] valueForKey:@"Service"];

        /*UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
         cell.accessoryView =
         [[[UIImageView alloc]
         initWithImage:indicatorImage]
         autorelease];*/
        /*NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector() object:nil];
         [thread setStackSize:44];
         [thread start];*/

        cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell.png"]]autorelease];
        // [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]] autorelease];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;



    }


    UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(19, 15, 75, 68)];
    imageView.contentMode = UIViewContentModeScaleToFill;

    // @synchronized(self)
    //{
    NSMutableDictionary *dict = [productInfoArray objectAtIndex:indexPath.row];
    if([dict objectForKey:@"friendImage"] == nil){
        imageView.backgroundColor = [UIColor clearColor];
        if ([dict objectForKey:@"isThreadLaunched"] == nil) {
            [NSThread detachNewThreadSelector:@selector(loadImagesInBackground:) toTarget:self withObject:[NSNumber numberWithInt:indexPath.row]];
            [dict setObject:@"Yes" forKey:@"isThreadLaunched"];
        }
    }else {

        imageView.image =[dict objectForKey:@"friendImage"];

    }

    //NSString *imagePath = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],@"no_image.png"];
    //imageView.layer.cornerRadius = 20.0;//vk
    //imageView.image=[UIImage imageWithContentsOfFile:imagePath];
    //imageView.layer.masksToBounds = YES;

    //imageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    //imageView.layer.borderWidth = 1.0;//vk
    //imageView.layer.cornerRadius=7.2f;
    [cell.contentView addSubview:imageView];
    //[imagePath release];
    [imageView release];

    imageView = nil;    
    //}

    UILabel *productCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 7, 60,20 )];
    productCodeLabel.textColor = [UIColor whiteColor];
    productCodeLabel.backgroundColor=[UIColor clearColor];
    productCodeLabel.text=[NSString stringWithFormat:@"%@",@"Code"];
    [cell.contentView addSubview:productCodeLabel];
    [productCodeLabel release];

    UILabel *CodeValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 7, 140,20 )];
    CodeValueLabel.textColor = [UIColor whiteColor];
    CodeValueLabel.backgroundColor=[UIColor clearColor];
    CodeValueLabel.text=[NSString stringWithFormat:@"%@",[[productInfoArray objectAtIndex:indexPath.row]valueForKey:@"ID"]];
    [cell.contentView addSubview:CodeValueLabel];
    [CodeValueLabel release];



    UILabel *productNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 35, 60,20 )];
    productNameLabel.textColor = [UIColor whiteColor];
    productNameLabel.backgroundColor=[UIColor clearColor];
    productNameLabel.text=[NSString stringWithFormat:@"%@",@"Name"];
    [cell.contentView addSubview:productNameLabel];
    [productNameLabel release];

    UILabel *NameValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 35, 140,20 )];
    NameValueLabel.textColor = [UIColor whiteColor];
    NameValueLabel.backgroundColor=[UIColor clearColor];
    NameValueLabel.text=[NSString stringWithFormat:@"%@",[[productInfoArray objectAtIndex:indexPath.row]valueForKey:@"Title"]];
    [cell.contentView addSubview:NameValueLabel];
    [NameValueLabel release];



    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 68, 60,20 )];
    dateLabel.textColor = [UIColor whiteColor];
    dateLabel.backgroundColor=[UIColor clearColor];
    dateLabel.text=[NSString stringWithFormat:@"%@",@"Date"];
    [cell.contentView addSubview:dateLabel];
    [dateLabel release];

    UILabel *dateValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 68, 140,20 )];
    dateValueLabel.textColor = [UIColor whiteColor];
    dateValueLabel.backgroundColor=[UIColor clearColor];
    dateValueLabel.text=[NSString stringWithFormat:@"%@",[[productInfoArray objectAtIndex:indexPath.row]valueForKey:@"PostedDate"]];
    dateValueLabel.font=[UIFont systemFontOfSize:14];
    dateValueLabel.numberOfLines=3;
    dateValueLabel.adjustsFontSizeToFitWidth=YES;
    [dateValueLabel setLineBreakMode:UILineBreakModeCharacterWrap];
    [cell.contentView addSubview:dateValueLabel];
    [dateValueLabel release];

}

请 - 2帮助我,我在做错误.....

2 个答案:

答案 0 :(得分:3)

我遇到了完全相同的问题(内存耗尽了大型图像列表)。两个建议:

  1. 使用https://github.com/rs/SDWebImage缓存图像 - 当您自动收到内存警告时,它会缓存到磁盘 - 释放不在视图中的图像,而代码则不会。

  2. 您的图片是小还是大?在大多数情况下,为表格视图设置缩略图是明智的 - 因为对于过大的图像来说,它可能很容易占用大量内存。

答案 1 :(得分:2)

这里没有什么特别神秘的事情发生。你得到一个你忽略的记忆警告。然后它会向您发送另一个警告,您也会忽略该警告。然后强制退出你的应用程序。

简而言之:你使用的内存太多了。

你需要找到一种减少使用方法,可能是将图像文件保存到“磁盘”,只将你想要显示的内容带到内存中,或者使用较少的图像或较小的图像。我确信你还有很多其他方法可以想到,但不知道你的应用程序做了什么,很难更具体。