调用CGAffineTransformMakeRotation时增加内存分配

时间:2014-06-18 13:25:14

标签: ios memory-leaks

我有一个集合视图,其布局设置为水平滚动。每个标题部分包含一个需要旋转90度的标签

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader)
    {
        Collection *collection=self.collectionArray[indexPath.section];

        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];


        headerView.tag=indexPath.section;
        headerView.backgroundColor=[Settings getInstance].textColor;
        UIImageView* expandView=(UIImageView*)[headerView viewWithTag:kExpandImage];
        expandView.image=[UIImage imageNamed:[NSString stringWithFormat:@"expand_%@.png",[Settings getInstance].appBrand]];


        UILabel* label=(UILabel*)[headerView viewWithTag:kCollectionLabel];
        label.text=[NSString stringWithFormat:@"%@",collection.name];
        label.backgroundColor=[Settings getInstance].textColor;
        label.textColor=[Settings getInstance].backColor;
        label.font=[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:50];
        label.transform = CGAffineTransformMakeRotation(-M_PI_2);


        CGRect frame=label.frame;
        frame.size.height=headerView.layer.frame.size.height;
        frame.origin.y=headerView.layer.frame.origin.y;
        label.frame=frame;


        //add tap gesture to detect touch
        HeaderTapRecognizer *singleTapRecogniser = [[HeaderTapRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
        singleTapRecogniser.sectionIndexPath=indexPath;
        singleTapRecogniser.delegate=self;
        singleTapRecogniser.numberOfTouchesRequired = 1;
        singleTapRecogniser.numberOfTapsRequired = 1;
        [headerView addGestureRecognizer:singleTapRecogniser];

        reusableview= headerView;

    }

    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

        reusableview = footerview;
    }

    return reusableview;
}

当我通过Instruments运行时,我在GSEventRunModal下获得了更多的内存分配。通过取出这行代码来解决问题

label.transform = CGAffineTransformMakeRotation(-M_PI_2);

我见过的所有例子都使用这种方法旋转标签,因此不确定我哪里出错了。

非常欢迎任何想法,谢谢。

1 个答案:

答案 0 :(得分:0)

要尝试的一件事是在添加文本,背景颜色,文本颜色和字体之前应用转换。这样你只能旋转一个空标签,然后再填充它。