简体   繁体   中英

iOS UICollectionView memory leak

I've created UICollectionView through storyboard. My cell is custom cell class that have 3 buttons with images. My images are available as part of class GalleryItemInfo. I have an array of those objects

[GalleryDataProvider sharedInstance].itemInfo

There is code for cellForItemAtIndexPath (in one cell there are three buttons for three items in array):

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

    if (indexPath.row % 2 == 0 && !is_iPhone) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellOrangeRed" forIndexPath:indexPath];
        if (is_Fingerprint_Version) {
            cell.imageViewRope.image = [UIImage imageNamed:@"image-rope-1.png"];
        }
    } else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellGreenBlue" forIndexPath:indexPath];
        if (is_Fingerprint_Version) {
            cell.imageViewRope.image = [UIImage imageNamed:@"image-rope-2.png"];
        }
    }

    cell.previewCellDelegate = self;
    cell.tag = indexPath.row;
    NSInteger leftPreviedId = [cell firstPreviewId];
    self.leftPreviewedID = leftPreviedId;

    UIImage *image1 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId]).slotPreviewImage;
    UIImage *image2;
    UIImage *image3;

    if (leftPreviedId + 1 < [[GalleryDataProvider sharedInstance].itemInfo count])
        image2 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId + 1]).slotPreviewImage;

    if (leftPreviedId + 2 < [[GalleryDataProvider sharedInstance].itemInfo count])
        image3 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId + 2]).slotPreviewImage;

    [cell setupWithImage1:image1 image2:image2 image3:image3];

    if (self.isEditModeEnabled) {
        [cell showRemoveButtons];
    } else {
        [cell hideRemoveButtons];
    }

    return cell;
}

Trouble: when I scroll my collection memory usage increases every swipe from right to left on about 1 megabyte.

Why memory is not released?

Update:

CollectionViewCellPreviewTriple code (created through storyboard):

#import <UIKit/UIKit.h>

@protocol UICollectionViewPreviewCellDelegate;


@interface CollectionViewCellPreviewTriple : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIButton *buttonSlot1;
@property (weak, nonatomic) IBOutlet UIButton *buttonSlot2;
@property (weak, nonatomic) IBOutlet UIButton *buttonSlot3;
@property (weak, nonatomic) IBOutlet UIButton *buttonRemove1;
@property (weak, nonatomic) IBOutlet UIButton *buttonRemove2;
@property (weak, nonatomic) IBOutlet UIButton *buttonRemove3;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewRope;


@property (nonatomic, weak) id<UICollectionViewPreviewCellDelegate> previewCellDelegate;

- (void)setupWithImage1:(UIImage *)image1 image2:(UIImage *)image2 image3:(UIImage *)image3;
- (void)showRemoveButtons;
- (void)hideRemoveButtons;

- (NSInteger)firstPreviewId;

@end


@protocol UICollectionViewPreviewCellDelegate

- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didSelectSubitemWithIndex:(NSInteger)subitemIndex;
- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didEditModeRequestWithStatus:(BOOL)status;
- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didRemoveRequestWithIndex:(NSInteger)subitemIndex;
- (void)slotButtonRequestsShadow:(UIButton *)slotButton;

@end

Update:

- (void)setupWithImage1:(UIImage *)image1 image2:(UIImage *)image2 image3:(UIImage *)image3
{
    [self.buttonSlot1 setBackgroundImage:image1 forState:UIControlStateNormal];
    [self.buttonSlot1 setBackgroundImage:image1 forState:UIControlStateHighlighted];

    //if (image2) {
    [self.buttonSlot2 setBackgroundImage:image2 forState:UIControlStateNormal];
    [self.buttonSlot2 setBackgroundImage:image2 forState:UIControlStateHighlighted];
    [self.buttonSlot2 setHidden:(image2 == nil)];
    //}
    //if (image3) {
    [self.buttonSlot3 setBackgroundImage:image3 forState:UIControlStateNormal];
    [self.buttonSlot3 setBackgroundImage:image3 forState:UIControlStateHighlighted];
    [self.buttonSlot3 setHidden:(image3 == nil)];
    //}
}

Profiling link for screen

This may be related to the issue that many people have been suffering with where the cell's are not reused.

To test this, you should override the method prepareForReuse and in it write a very simple log:

NSLog(@"%@ is being called as expected.", NSStringFromSelector(_cmd));

You should then run your app, scroll the collection view, and check the console to see if this log appears.

If this log does not appear, you may want to check this answer for help as to how to proceed. In my app cells are not reused in the simulator, but are reused on devices. It's odd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM