2011-01-10 84 views
1

嗨,朋友我從webservice獲取圖像,並在表格視圖中加載這些圖像。但是當我繼續滾動程序收到內存警告級別1,級別2,然後應用程序退出狀態爲0 。這種情況只發生在設備而不是模擬器中。 這是我的代碼,我正在幫助我。收到內存警告。級別= 2,程序接收到的信號:「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 *[email protected]"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幫我了,我在哪裏做錯誤.....

回答

3

我有完全相同的問題(內存不足大圖表列表)。兩個建議:

  1. 使用https://github.com/rs/SDWebImage緩存圖像 - 騰出圖像不在視野,你的代碼不 - 當你收到一封自動記憶警告它會緩存到磁盤。

  2. 你的照片是小還是大?在大多數情況下,爲表格視圖製作一個縮略圖是明智的 - 因爲對於尺寸過大的圖片來說,它很容易獲得大量內存。

2

沒有什麼特別神祕回事。你會得到一個內存警告,你忽略了。然後它會向您發送另一個警告,您也會忽略。然後它強制退出你的應用程序。

總之:您使用的內存太多。

您需要弄清楚一種少用的方法,或許將圖像文件保存爲「磁盤」,並只將您要顯示的內容帶入內存,或者使用較少的圖像或較小的圖像。我確信還有很多其他的方式可以想到,但不知道你的應用程序是否很難更具體。

+1

是的斯蒂芬你是正確的兄弟有一些內存問題,但我無法弄清楚。我什至把視圖控制器本地。我在這裏做什麼,我只是上傳一些圖像到服務器,然後但是當我繼續滾動表格視圖的應用程序Exits.Hey斯蒂芬給了我一些想法我該怎麼做親愛的..我要瘋了浪費了我的2天甚至改變了代碼,但沒有發生。 – Sabby 2011-01-10 12:04:15

+0

你似乎認爲有泄漏,但我不認爲是這種情況(至少在一個快速看,我沒有看到任何明顯的)。我想你只是用了太多的記憶。我建議你使用儀器來測量你使用的內存和在哪裏。然後你可以優化你的代碼,或者修復漏洞(如果有的話)。我不認爲這裏有一顆銀彈。 – 2011-01-10 12:23:27