1

我正在制作一个益智游戏,其中我在屏幕上添加了 35 个立方体。每个立方体有四个边(即 280 个边)。我在 CALayer 中添加图像,但是在玩了大约 20 次后,由于添加到 CALayer 的图像,我遇到了内存泄漏问题。如何释放 CALayer 中添加的旧图像所消耗的先前内存。

(void)setImageArr :(NSMutableArray*)img_arr ui_imgs:(NSMutableArray*)uiimgArr
{

    //[self.imgArr release];
    self.imgArr = [NSMutableArray array];//[[NSMutableArray alloc] init];
    [self.imgArr removeAllObjects];
    self.imgArr = img_arr;
    [img_arr release];
    img_arr = nil;

    [appDelegate().selfViewArr addObject:self.imgArr];

    self.uiimg_arr = [NSMutableArray array];//[[NSMutableArray alloc] init];
    [self.uiimg_arr removeAllObjects];
    self.uiimg_arr = uiimgArr;
    [uiimgArr release];
    uiimgArr = nil;

    [appDelegate().selfUIViewArr addObject:self.uiimg_arr];

    [self setupLayers];

}

(void)setupLayers
{

    float shiftWid, shiftHt;
    if(appDelegate().isRetinaDSPDetect)
    {
        shiftWid = 0.5f;
        shiftHt = 0.5f;
    }
    else
    {
        shiftWid = 0.0f;
        shiftHt = 0.0f;
    }

    CGFloat layerSize = Layer_Size;
    CGRect layerRect = CGRectMake(0.0, 0.0, layerSize + shiftWid, 134.6 + shiftHt); 


    UIImage *ui_img1 = [self.uiimg_arr objectAtIndex:0];
    CALayer *sideOne = [CALayer layer];

    sideOne.contents = (id)ui_img1.CGImage;

    sideOne.frame = layerRect;


    UIImage *ui_img2 = [self.uiimg_arr objectAtIndex:1];
    CALayer *sideTwo = [CALayer layer];

        sideTwo.contents = (id)ui_img2.CGImage;
    sideTwo.frame = layerRect;

    CGFloat degrees = 90.0;
    CGFloat radians = DegreesToRadians(degrees);
    CATransform3D rotation = CATransform3DMakeRotation(radians, 0.0, 1.0, 0.0);
    CATransform3D translation = CATransform3DMakeTranslation(Layer_Size/2, 0.0, Layer_Size/-2);
    CATransform3D position = CATransform3DConcat(rotation, translation);
    sideTwo.transform = position;

    //Side Three_Back_Red

    UIImage *ui_img3 = [self.uiimg_arr objectAtIndex:2];
    CALayer *sideThree = [CALayer layer];

    sideThree.contents = (id)ui_img3.CGImage;
    //sideThree.backgroundColor = [UIColor redColor].CGColor;
    sideThree.frame = layerRect;

    translation = CATransform3DMakeTranslation(0.0, 0.0, -Layer_Size);
    sideThree.transform = translation;

    //Side Four_Left_Yellow

    UIImage *ui_img4 = [self.uiimg_arr objectAtIndex:3];
    CALayer *sideFour = [CALayer layer];

    sideFour.contents = (id)ui_img4.CGImage;
    //sideFour.backgroundColor = [UIColor yellowColor].CGColor;
    sideFour.frame = layerRect;

    rotation = CATransform3DMakeRotation(radians, 0.0, 1.0, 0.0);
    translation = CATransform3DMakeTranslation(Layer_Size/-2, 0.0, Layer_Size/-2);
    sideFour.transform = CATransform3DConcat(rotation, translation);

    //Transform Layer

    [transformLayer addSublayer:sideOne];
    [transformLayer addSublayer:sideTwo];
    [transformLayer addSublayer:sideThree];
    [transformLayer addSublayer:sideFour];

    transformLayer.anchorPointZ = Layer_Size/-2;

}

上面给出的是我的代码。任何帮助将不胜感激。谢谢。

4

0 回答 0