Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIImage to CCSprite? [closed]

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"];
like image 360
SimplyKiwi Avatar asked Nov 11 '11 05:11

SimplyKiwi


2 Answers

Try this one:

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

uiImage is a UIImage object.

like image 155
banu Avatar answered Sep 19 '22 23:09

banu


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];
    }
like image 34
Sauvik Dolui Avatar answered Sep 21 '22 23:09

Sauvik Dolui