Search code examples
iosobjective-ccocos2d-iphone

CCSprite with image from URL in cocos2d v3


I'm trying to get Facebook profile image, but I'm not able to transform the data that I get from facebook into a Texture,

 NSData *data = [NSData dataWithContentsOfURL:url];
 UIImage *image = [UIImage imageWithData:data];
 NSLog(@"sprite nao existe");
 //convert UIImage to CCSprite
 **CCTexture *texture = [CCTexture textureWithFile:image];**


 CCSprite *sprite = [CCSprite spriteWithTexture:texture];
 sprite.position = ccp(winWidth*.5, winHeight*.5)

In the line CCTexture *text... I'm getting this warning, which make the simulator stops/crashes:

Incompatible pointer types sending 'UIImage' to parameter of type NSString

This is the message when the simulator stops in the Log

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'TextureCache: fileimage MUST not be nil'
*** First throw call stack:

Solution

  • CCTexture *texture = [CCTexture textureWithFile:image];
    

    Here image must be a NSString not a UIImage. The reference will tell you that.

    Instead use this method:

    - (id)initWithCGImage:(CGImageRef)cgImage contentScale:(CGFloat)contentScale
    

    Like so:

    CCTexture *texture = [[CCTexture alloc] initWithCGImage:image.CGImage 
                                               contentScale:image.scale];