Add UIImage to CCSprite?

I don’t know if it is stupid or not, but I turn my head over it for about an hour or two.

I get the NSData object from NSUserDefaults and then convert it to UIImage . Now, how to install my CCSprite image on my UIImage?

spriteWithFile does not work here because it requires an NSString . I am looking for an API that has a UIImage parameter. Is it possible?

Any ideas?

Edit1 : How is it?

 UIImage *image = [UIImage imageWithData:(NSData*)obj]; sprite = [CCSprite spriteWithCGImage:image.CGImage key:@"mainSprite"]; 
+6
source share
2 answers

Try the following:

 CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease]; CCSprite *sprite = [CCSprite spriteWithTexture:tex]; 

uiImage - an UIImage object.

+10
source

This method works great for me ...

  -(CCSprite*)spriteFromDiskFileName:(NSString*)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); NSString *directoryPath = [paths objectAtIndex:0] ; NSData *imageData = [[NSData alloc]initWithContentsOfFile:[directoryPath stringByAppendingPathComponent:fileName]]; UIImage *spriteImage = [UIImage imageWithData:imageData]; if (spriteImage) { printf("\nUIImage created from disk image \n"); } else { printf("\nUIImage creation failed from disk image \n"); } CCTexture2D *texture = [[CCTexture2D alloc] initWithCGImage:spriteImage.CGImage resolutionType:kCCResolutionUnknown]; return [CCSprite spriteWithTexture:texture]; } 
+1
source

Source: https://habr.com/ru/post/901275/


All Articles