核心动画在设备上访问不良

use*_*102 1 xcode core-animation exc-bad-access objective-c automatic-ref-counting

我正在尝试使用CAlayers进行逐帧动画.我正在使用本教程http://mysterycoconut.com/blog/2011/01/cag1/进行此操作,但一切都适用于禁用ARC,当我尝试用ARC重写代码时,它可以在模拟器上完美地工作但在设备上我的访问内存不好.

Layer Class接口

#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
Run Code Online (Sandbox Code Playgroud)

图层类实现

@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];  // IN THIS LINE IS BAD ACCESS
    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
Run Code Online (Sandbox Code Playgroud)

我在viewDidAppear上创建了图层,并通过单击按钮开始动画,但在初始化后我遇到了错误的访问错误

    -(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"];
    }

}
Run Code Online (Sandbox Code Playgroud)

你知道如何解决它吗?非常感谢.

dan*_*dee 5

TL; DR

你的崩溃来自于悬空CGImageRef.

您可以通过更改线路来修复它

richter = [MCSpriteLayer layerWithImage:richterImg sampleSize:fixedSize];
Run Code Online (Sandbox Code Playgroud)

richter = [MCSpriteLayer layerWithImage:[[UIImage imageNamed:@"mama_default"] CGImage] sampleSize:fixedSize];
Run Code Online (Sandbox Code Playgroud)

或许多等效选项.

说明

为了解释发生了什么,我将略微修改并注释您的实现viewWillAppear::

- (void)viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];

    // 1
    UIImage *richterImage = [UIImage imageNamed:@"mama_default"];
    // 2
    CGImageRef backingImage = richterImage.CGImage;
    CGSize fixedSize = animacja.frame.size;

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

    [self.view.layer addSublayer:richter];
}
Run Code Online (Sandbox Code Playgroud)

我们来看看评论:

  1. 您向UIImage班级询问图像,并通过返回非拥有参考来回答问题.
  2. 您要求该图像用于其原始后备存储(CGImage),并通过将非拥有引用返回到该存储的内存来应答.注意,这不是 Objective-C对象!
  3. 您使用对该后备存储的非拥有引用作为创建精灵层的参数.

在手动引用计数下,步骤1中的对象位于最近的封闭自动释放池中.因为您在代码附近没有看到任何一个,所以隐含地保证该对象比您的实现范围更长viewDidAppear:.

在ARC下,情况并非如此:
因为在步骤2之后从未引用在步骤1中返回的对象,所以该对象可能在第二步之后消失任何行,这是对它的最后一次引用.并且因为后备存储不是 Objective-C对象,所以在以后的任何时候引用它而没有明确声明其中的兴趣(通过CGImageRetain例如),它一旦变为无效UIImage变为无效.

因为ARC下的编译器插入到该特殊函数的调用功能上等同于方法retain,releaseautorelease常数,它可以优化一些冗余的保留/(自)远离释放对,从而减少消息开销,和某些寿命对象.

然而,它如何积极地优化它们是一个实现细节,并取决于许多参数.因此,当您调用时layerWithImage:sampleSize:,(其声明为CGImageRef self.contents = (__bridge id)img;)您传入的第一个参数可能是可能不是指向无效内存的指针,具体取决于编译器对代码的优化程度.