ios内存分配问题截图

ios内存分配问题截图,ios,animation,memory-management,screenshot,ibooks,Ios,Animation,Memory Management,Screenshot,Ibooks,我正在测试该项目: 我在截图时注意到内存分配的增长 当我评测该应用程序时,我看到很多内存将在CALayer renderInContext中分配。原因似乎是CGContextDrawImage。但如何解决这个问题呢 我将在此处粘贴必要的代码: @implementation iBookAnimationViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

我正在测试该项目: 我在截图时注意到内存分配的增长

当我评测该应用程序时,我看到很多内存将在CALayer renderInContext中分配。原因似乎是CGContextDrawImage。但如何解决这个问题呢

我将在此处粘贴必要的代码:

@implementation iBookAnimationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.layer.contents = (id)[UIImage imageNamed:@"Shelf_Contents@2x.png"].CGImage;

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip:)];
    [self.view addGestureRecognizer:gesture];
    [gesture release];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark Gesture

- (void)flip:(UIGestureRecognizer*)gesture{

    FlipSideViewController *controller = [[FlipSideViewController alloc] init];
    controller.delegate = (id<FlipSideDelegate>)self;
    [self presentModalViewController:controller animated:YES];
    [controller release];

}


#pragma mark -
#pragma mark Animation Methods

- (CATransform3D)transformWithDegrees:(CGFloat)degrees{

    if (degrees == 0) {
        return CATransform3DIdentity;
    }

    return CATransform3DMakeRotation((M_PI/180)*degrees, 0.0f, 1.0f, 0.0);

}

- (UIImage*)contentsForView:(UIView*)aView{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

    if (flag) {
        [[anim valueForKey:@"ContentsLayer"] removeFromSuperlayer];
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }

}

- (void)animateFromViewController:(UIViewController*)fromController toViewController:(UIViewController*)toController{

    UIImage *image = [self contentsForView:toController.view];

    CALayer *contentsLayer = [CALayer layer];
    contentsLayer.frame = toController.view.bounds;
    contentsLayer.backgroundColor = [UIColor blackColor].CGColor;
    [toController.view.layer addSublayer:contentsLayer];

    CATransformLayer *transformLayer = [CATransformLayer layer];
    transformLayer.frame = contentsLayer.bounds;
    [contentsLayer addSublayer:transformLayer];

    CALayer *backLayer = [CALayer layer];
    backLayer.contents = (id)image.CGImage;
    backLayer.doubleSided = NO;
    backLayer.frame = transformLayer.bounds;
    backLayer.zPosition = 0.0f;
    backLayer.transform = [self transformWithDegrees:180.0f];
    [transformLayer addSublayer:backLayer];

    CALayer *frontLayer = [CALayer layer];
    frontLayer.contents = (id)[self contentsForView:fromController.view].CGImage;
    frontLayer.doubleSided = NO;
    frontLayer.zPosition = FLIP_GAP;
    frontLayer.frame = transformLayer.bounds;
    frontLayer.masksToBounds = NO;
    [transformLayer addSublayer:frontLayer];

    CALayer *sideLayer = [CALayer layer];
    sideLayer.contents = (id)[UIImage imageNamed:@"Wood Tile.png"].CGImage;
    sideLayer.zPosition = FLIP_GAP/2;
    sideLayer.frame = CGRectMake(transformLayer.bounds.origin.x-(FLIP_GAP/2), transformLayer.bounds.origin.y, FLIP_GAP, transformLayer.bounds.size.height);
    sideLayer.transform = [self transformWithDegrees:90.0f];
    [transformLayer addSublayer:sideLayer];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform"];
    animation.toValue = [NSValue valueWithCATransform3D:[self transformWithDegrees:179.9f]];
    animation.duration = ANIMATION_DURATION;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.delegate = self;
    [animation setValue:contentsLayer forKey:@"ContentsLayer"];
    [transformLayer addAnimation:animation forKey:@"FlipAnimation"];

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

}


#pragma mark -
#pragma mark Modal Prentation Methods

- (void)dismissModalViewControllerAnimated:(BOOL)animated{

    UIViewController *modalViewController = self.modalViewController;
    [self animateFromViewController:modalViewController toViewController:self];
    [super dismissModalViewControllerAnimated:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];


}

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated{

    [super presentModalViewController:modalViewController animated:NO];
    [self animateFromViewController:self toViewController:modalViewController];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];

}


#pragma mark -
#pragma mark FlipSideDelegate

- (void)dismissFlipSideViewController:(FlipSideViewController*)controller{

    [self dismissModalViewControllerAnimated:YES];

}


#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [super dealloc];
}

@end
@implementation iBookAnimationViewController
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
self.view.layer.contents=(id)[UIImage ImageName:@“Shelf”_Contents@2x.png“].CGImage;
UITapGestureRecognizer*手势=[[UITapGestureRecognizer alloc]initWithTarget:自我操作:@selector(翻转:)];
[self.view addgestureRecognitor:signature];
[手势释放];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackObaque];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#布拉格标记-
#布拉格标记手势
-(无效)翻转:(UIgestureRecognitor*)手势{
FlipSideViewController*控制器=[[FlipSideViewController alloc]init];
controller.delegate=(id)self;
[self-presentModalViewController:控制器已设置动画:是];
[控制器释放];
}
#布拉格标记-
#pragma标记动画方法
-(CATTransformorM3D)带度数的变换:(CGFloat)度数{
如果(度==0){
返回CATTransferM3DidEntity;
}
返回CATTransformerM3dMake旋转((M_PI/180)*度,0.0f,1.0f,0.0);
}
-(UIImage*)内容查看:(UIView*)查看{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,是,[[UIScreen mainScreen]缩放]);
[aView.layer renderContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回图像;
}
-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)标志{
国际单项体育联合会(旗){
[[anim valueForKey:@“ContentsLayer”]removeFromSuperlayer];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
}
-(void)animateFromViewController:(UIViewController*)从控制器到ViewController:(UIViewController*)到控制器{
UIImage*image=[self-contentsForView:toController.view];
CALayer*contentsLayer=[CALayer层];
contentsLayer.frame=toController.view.bounds;
contentsLayer.backgroundColor=[UIColor blackColor].CGColor;
[toController.view.layer addSublayer:contentsLayer];
CATTransformLayer*transformLayer=[CATTransformLayer层];
transformLayer.frame=contentsLayer.bounds;
[contentsLayer addSublayer:transformLayer];
CALayer*backLayer=[CALayer layer];
backLayer.contents=(id)image.CGImage;
backLayer.doubleside=否;
backLayer.frame=transformLayer.bounds;
backLayer.zPosition=0.0f;
backLayer.transform=[self transformWithDegrees:180.0f];
[转换层添加子层:背层];
CALayer*frontLayer=[CALayer layer];
frontLayer.contents=(id)[self-contentsForView:fromController.view].CGImage;
frontLayer.doubleside=否;
frontLayer.zPosition=翻转间隙;
frontLayer.frame=transformLayer.bounds;
frontLayer.masksToBounds=否;
[转换层添加子层:前层];
CALayer*侧层=[CALayer层];
sideLayer.contents=(id)[UIImage ImageName:@“Wood Tile.png”].CGImage;
sideLayer.zPosition=翻转间隙/2;
sideLayer.frame=CGRectMake(transformLayer.bounds.origin.x-(翻转间隙/2),transformLayer.bounds.origin.y,翻转间隙,transformLayer.bounds.size.height);
sideLayer.transform=[self transformWithDegrees:90.0f];
[转换层添加子层:侧层];
CABasicAnimation*动画=[CABasicAnimation animationWithKeyPath:@“子层转换”];
animation.toValue=[NSValue ValueWithCatTransformM3D:[self-transformWithDegrees:179.9f]];
animation.duration=动画\持续时间;
animation.fillMode=kCAFillModeForwards;
animation.removedOnCompletion=否;
animation.delegate=self;
[动画设置值:contentsLayer-forKey:@“contentsLayer”];
[transformLayer addAnimation:animation forKey:@“FlipAnimation”];
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
}
#布拉格标记-
#pragma-mark模态预测方法
-(void)dismissModalViewControllerAnimated:(BOOL)动画{
UIViewController*modalViewController=self.modalViewController;
[self-animateFromViewController:modalViewController到ViewController:self];
[super dismissModalViewControllerAnimated:否];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlack:YES];
}
-(void)presentModalViewController:(UIViewController*)已设置动画的modalViewController:(BOOL)已设置动画{
[超级呈现modalViewController:modalViewController动画:否];
[self-animateFromViewController:self-toViewController:modalViewController];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault动画:是];
}
#布拉格标记-
#pragma标记FlipSideDelegate
-(无效)解除FlipSideViewController:(FlipSideViewController*)控制器{
[自我解散Modalviewcontrolleranimated:是];
}
#布拉格标记-
#pragma标记内存管理
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(无效)视图卸载{
}
-(无效)解除锁定{
[super dealoc];
}
@结束

尝试使用带有“内存泄漏”工具的仪器运行您的确切用例。它很可能会指向一把冒烟的枪,而且很难找到。此外,我总是推荐bbum关于堆分析的优秀链接(这在过去救了我一命)