【问题标题】:UICollectionView Memory Leak CrashUICollectionView 内存泄漏崩溃
【发布时间】:2015-03-14 16:18:53
【问题描述】:

我的 CollectionView 正在从 Parse 网络检索图像,并且不断崩溃。在崩溃之前,我不断收到内存警告。我猜我需要释放空间。但问题是我对编码比较陌生,所以我不知道该怎么做。我什至不知道需要从哪里释放内存。有人可以帮我理解我在代码中做错了什么吗?

         - (void)viewDidLoad {
  [super viewDidLoad];


    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar1.png"] forBarMetrics:UIBarMetricsDefault];


   // self.navigationController.hidesBarsOnSwipe = true;

    [self queryParseMethodCell];

     // Do any additional setup after loading the view.
}


- (void)queryParseMethodCell {
    NSLog(@"start Money");
    PFQuery *query3 = [PFQuery queryWithClassName:@"MainWall"];
    PFQuery *query4 = [PFQuery queryWithClassName:@"MainWall"];

    [query3 orderByDescending:@"createdAt"];
    [query4 orderByAscending:@"createdAt"];

    [query3 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)     {
    if (!error) {
        NSLog(@"check bobby");

        imageFilesArray2 = [[NSArray alloc] initWithArray:objects];

        [self.HomePost reloadData];

    }
    else{

        NSLog(@"NO Good33");
    }


}];
    [query4 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"check bobby");

        imageFilesArray3 = [[NSArray alloc] initWithArray:objects];

        [self.HomePost reloadData];

    }
    else{

        NSLog(@"NO Good44");
    }


   }];

  }

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {


   if (self.lastContentOffset > scrollView.contentOffset.y) {
      [self.navigationController setNavigationBarHidden:NO animated:YES];

   }

   else if (self.lastContentOffset < scrollView.contentOffset.y){

    [self.navigationController setNavigationBarHidden:YES animated:YES];


   }

   else {

       NSLog(@"Andy Error1");

   }

}

- (IBAction)SwipeLeft:(UISwipeGestureRecognizer *)sender {

    [self performSegueWithIdentifier:@"Notifications" sender:self];

}
- (IBAction)SwipeRight:(UISwipeGestureRecognizer *)sender{

    [self performSegueWithIdentifier:@"FavWall" sender:self];

   }


- (IBAction)MainWall:(id)sender{

    [self performSegueWithIdentifier:@"MainWall" sender:self];

}

- (IBAction)FavWall:(id)sender{

    [self performSegueWithIdentifier:@"FavWall" sender:self];

}

- (IBAction)Notifications:(id)sender{

    [self performSegueWithIdentifier:@"Notifications" sender:self];

}

- (IBAction)Home:(id)sender{

    [self performSegueWithIdentifier:@"Home" sender:self];

}

- (IBAction)YourAccount:(id)sender{

    [self performSegueWithIdentifier:@"YourAccount" sender:self];

}

- (IBAction)UsersAccount:(id)sender{

    [self performSegueWithIdentifier:@"UsersAccount" sender:self];

}

-(IBAction)PicTake:(id)sender{


    [self performSegueWithIdentifier:@"Camera" sender:self];

 }





#pragma mark Collection View
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return [imageFilesArray2 count];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *cellIdentifier = @"TopCell";

    DiscoverCell *cell = nil;

    if (cell == nil) {

        cell = (DiscoverCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    }
    else {

        NSLog(@"2Error");
    }



    PFObject *imageObject2 = [imageFilesArray2 objectAtIndex:indexPath.row];
    cell.Username.text = [imageObject2 objectForKey:@"Name"];


    PFFile *imageFile3 = [imageObject2 objectForKey:@"ProfilePicture"];



    [imageFile3 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        NSLog(@"CheckCash");




      //  cell.TopStory.image = [UIImage imageWithData:data];
        cell.HomeImg.image = [UIImage imageWithData:data];
        cell.Back.image = [UIImage imageWithData:data];
        //cell.HomeImg2.image = [UIImage imageWithData:data];




    }
    else{

        NSLog(@"NO Good");
    }

    cell.HomeImg.layer.cornerRadius = cell.HomeImg.frame.size.width /2;
    cell.HomeImg.clipsToBounds = YES;
    cell.Back.image = cell.HomeImg.image;

    }];

    PFObject *imageObject5 = [imageFilesArray3 objectAtIndex:indexPath.row];
    cell.Username.text = [imageObject5 objectForKey:@"Name"];


    PFFile *imageFile4 = [imageObject5 objectForKey:@"ProfilePicture"];

    [imageFile4 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            NSLog(@"CheckCash");




            cell.TopStory.image = [UIImage imageWithData:data];



    }
    else{

        NSLog(@"NO Good22");
    }


}];


    return cell;

}



 - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

【问题讨论】:

  • 不要在你的问题中表示感谢。我们希望问题尽可能简单。如果每个人都感谢,那就很烦人了:)
  • 空格太多,无法读取。

标签: objective-c xcode memory-management parse-platform uicollectionview


【解决方案1】:

您有内存泄漏。每次滚动所有图像都会重新加载。这样的事情应该可以解决问题

  //at start For every data DataisNotLoaded should be true

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


if dataisNotLoaded[index] {
       DataisNotLoaded[index] = false

[imageFile3 getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        NSLog(@"CheckCash");




      //  cell.TopStory.image = [UIImage imageWithData:data];
        cell.HomeImg.image = [UIImage imageWithData:data];
        cell.Back.image = [UIImage imageWithData:data];
        //cell.HomeImg2.image = [UIImage imageWithData:data];




    }
    else{

        NSLog(@"NO Good");
    }

 }

【讨论】:

  • 如果 dataisNotLoaded ?我怎么称呼它?
  • 你知道是什么导致了循环吗?
  • 造成循环的原因是什么意思
  • 您可以通过在 cellForItemAtIndexPath 中写入 println 来检查它,看看它何时被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2020-09-07
  • 1970-01-01
  • 2018-04-04
  • 2013-02-04
  • 2016-07-14
相关资源
最近更新 更多