2

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:
James Webster
  • 31,873
  • 11
  • 70
  • 114
Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • The question & answer you wrote are so good to know. So, I translated them to share with Korean developers. If you mind it, please let me know and it will be deleted at [ctrlaltdel](http://ctrlaltdel.co.kr) – Keith Park Aug 29 '14 at 06:39
  • @KeithPark thanks! I don't mind you translate the question, just make some reference to this source please. – Filipe Ferminiano Aug 31 '14 at 12:02

3 Answers3

2
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];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks, but I'm getting this message: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid texture for sprite' – Filipe Ferminiano Mar 27 '14 at 12:36
  • 1
    And you are sure this time that the data retrieved and the image created is a valid image and not a nil object? You may want to try Xcode "QuickLook" at the UIImage to find out (select the image variable in debugger and click the 'eye' icon): https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/CH01-quick_look_for_custom_objects/CH01-quick_look_for_custom_objects.html – CodeSmile Mar 27 '14 at 12:54
0

Try below code to set image in your Sprite

NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
CCSprite *sprite = [CCSprite spriteWithCGImage:image.CGImage key:@"BB"];
sprite.position = ccp(winWidth*.5, winHeight*.5)
[self addChild:sprite];
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
0

Yes @LearnCocos2D, you are absolutely right. I have created a category on CCSprite and created a method named

+ (id)spriteWithURL:(NSString*)imageURL defaultSprite:(NSString*)defaultImage;

. It simply creates a CCSprite from the defaultImage and if download of image with the imageURL completes, it automatically sets the texture of CCSprite from the downloaded image. You can find out the whole project from GitHub. Hope this will help you.

Sauvik Dolui
  • 5,520
  • 3
  • 34
  • 44