Core Animation bad access on device

Posted by user1595102 on Stack Overflow See other posts from Stack Overflow or by user1595102
Published on 2012-10-13T09:32:49Z Indexed on 2012/10/13 9:36 UTC
Read the original article Hit count: 261

I'm trying to do a frame by frame animation with CAlayers. I'm doing this with this tutorial http://mysterycoconut.com/blog/2011/01/cag1/ but everything works with disable ARC, when I'm try to rewrite code with ARC, it's works on simulator perfectly but on device I got a bad access memory.

Layer Class interface

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface MCSpriteLayer : CALayer {
    unsigned int sampleIndex;
}

// SampleIndex needs to be > 0
@property (readwrite, nonatomic) unsigned int sampleIndex; 

// For use with sample rects set by the delegate
+ (id)layerWithImage:(CGImageRef)img;
- (id)initWithImage:(CGImageRef)img;

// If all samples are the same size 
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size;
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;

// Use this method instead of sprite.sampleIndex to obtain the index currently displayed on screen
- (unsigned int)currentSampleIndex; 


@end

Layer Class implementation

@implementation MCSpriteLayer

@synthesize sampleIndex;

- (id)initWithImage:(CGImageRef)img;
{
    self = [super init];
    if (self != nil)
    {
        self.contents = (__bridge id)img;
        sampleIndex = 1;
    }

    return self;
}

+ (id)layerWithImage:(CGImageRef)img;
{
    MCSpriteLayer *layer = [(MCSpriteLayer*)[self alloc] initWithImage:img];
    return layer ;
}


- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
    self = [self initWithImage:img]; 
    if (self != nil)
    {
        CGSize sampleSizeNormalized = CGSizeMake(size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img));
        self.bounds = CGRectMake( 0, 0, size.width, size.height );
        self.contentsRect = CGRectMake( 0, 0, sampleSizeNormalized.width, sampleSizeNormalized.height );
    }

    return self;
}


+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
    MCSpriteLayer *layer = [[self alloc] initWithImage:img sampleSize:size];
    return layer;
}


+ (BOOL)needsDisplayForKey:(NSString *)key;
{
    return [key isEqualToString:@"sampleIndex"];
}

// contentsRect or bounds changes are not animated
+ (id < CAAction >)defaultActionForKey:(NSString *)aKey;
{
    if ([aKey isEqualToString:@"contentsRect"] || [aKey isEqualToString:@"bounds"])
        return (id < CAAction >)[NSNull null];

    return [super defaultActionForKey:aKey];
}


- (unsigned int)currentSampleIndex;
{
    return ((MCSpriteLayer*)[self presentationLayer]).sampleIndex;
}


// Implement displayLayer: on the delegate to override how sample rectangles are calculated; remember to use currentSampleIndex, ignore sampleIndex == 0, and set the layer's bounds
- (void)display;
{
    if ([self.delegate respondsToSelector:@selector(displayLayer:)])
    {
        [self.delegate displayLayer:self];
        return;
    }

    unsigned int currentSampleIndex = [self currentSampleIndex];
    if (!currentSampleIndex)
        return;

    CGSize sampleSize = self.contentsRect.size;
    self.contentsRect = CGRectMake(
        ((currentSampleIndex - 1) % (int)(1/sampleSize.width)) * sampleSize.width, 
        ((currentSampleIndex - 1) / (int)(1/sampleSize.width)) * sampleSize.height, 
        sampleSize.width, sampleSize.height
    );
}


@end

I create the layer on viewDidAppear and start animate by clicking on button, but after init I got a bad access error

    -(void)viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mama_default.png" ofType:nil];
    CGImageRef richterImg = [UIImage imageWithContentsOfFile:path].CGImage;
    CGSize fixedSize = animacja.frame.size;
    NSLog(@"wid: %f, heigh: %f", animacja.frame.size.width, animacja.frame.size.height);
    NSLog(@"%f", animacja.frame.size.width);

    richter = [MCSpriteLayer layerWithImage:richterImg sampleSize:fixedSize];
    animacja.hidden = 1;
    richter.position = animacja.center;

    [self.view.layer addSublayer:richter];
}

-(IBAction)animacja:(id)sender
{
    if ([richter animationForKey:@"sampleIndex"])
    {NSLog(@"jest");
    }
    if (! [richter animationForKey:@"sampleIndex"])
    {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
    anim.fromValue = [NSNumber numberWithInt:0];
    anim.toValue = [NSNumber numberWithInt:22];
    anim.duration = 4;
    anim.repeatCount = 1;

    [richter addAnimation:anim forKey:@"sampleIndex"];
    }

}

Have you got any idea how to fix it? Thanks a lot.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about xcode