簡體   English   中英

帶有來自 cocos2d v3 中 URL 的圖像的 CCSprite

[英]CCSprite with image from URL in cocos2d v3

我正在嘗試獲取 Facebook 個人資料圖片,但我無法將從 Facebook 獲取的數據轉換為紋理,

 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)

在 CCTexture *text... 行中,我收到此警告,這會使模擬器停止/崩潰:

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

這是模擬器在日志中停止時的消息

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'TextureCache: fileimage MUST not be nil'
*** First throw call stack:
CCTexture *texture = [CCTexture textureWithFile:image];

這里的圖像必須是一個 NSString 而不是 UIImage。 參考資料會告訴你。

而是使用此方法:

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

像這樣:

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

嘗試以下代碼在您的 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];

是的@LearnCocos2D,你說得對。 我在 CCSprite 上創建了一個類別並創建了一個名為的方法

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

. 它只是從defaultImage創建一個 CCSprite,如果使用imageURL下載圖像完成,它會自動從下載的圖像設置 CCSprite 的紋理。 你可以從GitHub找到整個項目。 希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM