I do not know if this is stupid or not but I have been scratching my head over this for about an hour or two now.
I am getting a NSData
object from NSUserDefaults
then converting it to a UIImage
. Now how do I set of the image of my CCSprite
to my UIImage?
spriteWithFile
doesn't work here since it requires a NSString
. I am looking for an API that has a parameter of a UIImage
. Is this possible?
Any ideas?
Edit1: How is this?
UIImage *image = [UIImage imageWithData:(NSData*)obj];
sprite = [CCSprite spriteWithCGImage:image.CGImage key:@"mainSprite"];
Try this one:
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease];
CCSprite *sprite = [CCSprite spriteWithTexture:tex];
uiImage is a UIImage object.
This method is working perfectly 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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With