iosxcodecalayercaemitterlayercaemittercell

CAEmitterLayer: Using an @2x (retina) png image with CAEmitterCell


Using CAEmitterLayer, @2x (retina) images aren't being scaled property like they do elsewhere in iOS. The result I'm getting is the @2x version is being displayed 4x the size of the non-retina image instead of being scaled down.

Any idea of how to fix this? I have tried testing the image pain in a UIImageView and the results are as they should be, so this appears to be a problem with CAEmitterLayer and CAEmitterCell. The images have the correct @2x.png specifier.

Here's the code I'm using :

CAEmitterLayer *fallingCoinEmitter = [CAEmitterLayer layer];
fallingCoinEmitter.emitterPosition = CGPointMake(self.view.bounds.size.width / 2.0, -30);
fallingCoinEmitter.emitterSize = CGSizeMake(self.view.bounds.size.width * 2.0, 0.0);;

    // Spawn points for the flakes are within on the outline of the line
fallingCoinEmitter.emitterMode  = kCAEmitterLayerOutline;
fallingCoinEmitter.emitterShape = kCAEmitterLayerLine;

    // Configure the snowflake emitter cell
CAEmitterCell *coin = [CAEmitterCell emitterCell];
coin.birthRate      = 8.0;
coin.lifetime       = 5.0;
coin.velocity       = -180;             // falling down slowly
coin.velocityRange = 80;
coin.yAcceleration = 40;
coin.emissionRange = 0.4 * M_PI;        // some variation in angle
coin.spinRange      = 0.45 * M_PI;      // slow spin
coin.contents       = (id) [[UIImage imageNamed:@"Coin_Generic_Emitter"] CGImage];
coin.scale          = 1.0;
coin.scaleRange     = 0.0;

    // Make the flakes seem inset in the background
fallingCoinEmitter.shadowOpacity = 1.0;
fallingCoinEmitter.shadowRadius  = 4.0;
fallingCoinEmitter.shadowOffset  = CGSizeMake(0.0, 3.0);
UIColor *darkGreenColor = [UIColor colorWithRed:0.005 green:0.163 blue:0.005 alpha:1.000];
fallingCoinEmitter.shadowColor   = [darkGreenColor CGColor];
[fallingCoinEmitter setContentsScale:[UIScreen mainScreen].scale];
//fallingCoinEmitter.shouldRasterize = YES;
//[fallingCoinEmitter setRasterizationScale:[UIScreen mainScreen].scale];
//fallingCoinEmitter.scale = fallingCoinEmitter.scale / [[UIScreen mainScreen] scale];

    // Add everything to our backing layer below the UIContol defined in the storyboard
fallingCoinEmitter.emitterCells = [NSArray arrayWithObject:coin];
[self.view.layer insertSublayer:fallingCoinEmitter atIndex:0];

Thanks!

Update:

@Fabian, setting contentScale isn't working, at least not my solution

    [fallingCoinEmitter setContentsScale:[UIScreen mainScreen].scale];

I also tried this with no results..

    emitter.shouldRasterize = YES;
    [emitter setRasterizationScale:[UIScreen mainScreen].scale];

And settings the scale range did not work. There's still a difference between the sizes on the iPad 2 and 3 (w RD).


Solution

  • You should try modifying your CAEmmitterCells' scale and scaleRange properties depending on the device's screen.

    cell.scale = cell.scale / [[UIScreen mainScreen] scale];