将图像添加到CALayer时出现内存泄漏问题

时间:2013-10-01 09:03:14

标签: ios objective-c memory-leaks

我正在制作一款益智游戏,我在屏幕上添加了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;

}

以上是我的代码。任何帮助将受到高度赞赏。 谢谢。

0 个答案:

没有答案