UICollectionView内存泄漏崩溃

时间:2015-03-14 16:18:54

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

我的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

1 个答案:

答案 0 :(得分:0)

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

  //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");
    }

 }