SKTexture from UIImage looks different

2019-09-04 14:45发布

I'm playing around with SpriteKit and I'm creating a SKTexture from an UIImage, then using SKSpriteNode to add the child to my SKScene (As a background), everything works fine, except that the UIImage looks very different from the original image, I tried to recreate the image in photoshop and the issue still remains, tested on Simulator and real device and different image colors, no changes.

The image format is PNG, and I added through Images.xcassets in Xcode 5

Image, results:

enter image description here

Different image, results:

enter image description here

I'm using the following code in my SKScene subclass:

- (id)initWithSize:(CGSize)size
{

    if (self = [super initWithSize:size]) {

        SKTexture *textureGradient = [SKTexture textureWithImage:[UIImage imageNamed:@"Image"]];
        SKSpriteNode* spriteGradient = [SKSpriteNode spriteNodeWithTexture:textureGradient];
        [self addChild:spriteGradient];

    }
    return self;
}

What I'm doing wrong?

Any help would be appreciated. :)

1条回答
The star\"
2楼-- · 2019-09-04 15:20

Solution:

It was my fault, the image position was wrong, so I changed the position property:

SKTexture *textureGradient = [SKTexture textureWithImage:[UIImage imageNamed:@"Image"]];
SKSpriteNode* spriteGradient = [SKSpriteNode spriteNodeWithTexture:textureGradient];
spriteGradient.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
[self addChild:spriteGradient];
查看更多
登录 后发表回答