I have just implemented multiple photo selection from iphone photo library and i am saving all selected photo in document directory every time as a array, now i want to show all saved images in my class from document directory as a thumbnail, i have tried some logic but my game getting crashing, My code is below. Any help will be appreciate. Thanks in advance.

-(id) init
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value

    if( (self=[super init])) {

        CCSprite *photoalbumbg = [CCSprite spriteWithFile:@"photoalbumbg.png"];
        photoalbumbg.anchorPoint = ccp(0,0);
        [self addChild:photoalbumbg z:0];

        //Background Sound
    //    [[SimpleAudioEngine sharedEngine]playBackgroundMusic:@"Background Music.wav" loop:YES];

        CCSprite *photoalbumframe = [CCSprite spriteWithFile:@"photoalbumframe.png"];
        photoalbumframe.position = ccp(160,240);
        [self addChild:photoalbumframe z:2];

        CCSprite *frame = [CCSprite spriteWithFile:@"Photo-Frames.png"];
        frame.position = ccp(160,270);
        [self addChild:frame z:1];


        CCMenuItemImage * upgradebtn = [CCMenuItemImage itemFromNormalImage:@"AlbumUpgrade.png"

        CCMenu * fMenu = [CCMenu menuWithItems:upgradebtn,nil];
        fMenu.position = ccp(200,110);
        [self addChild:fMenu z:3];

        NSError *error;
        NSFileManager *fM = [NSFileManager defaultManager];
        NSString *documentsDirectory = [NSHomeDirectory()
        NSLog(@"Documents directory: %@",
              [fM contentsOfDirectoryAtPath:documentsDirectory error:&error]);

     NSArray *allfiles = [fM contentsOfDirectoryAtPath :documentsDirectory error:&error];

        directoryList = [[NSMutableArray alloc] init];

        for(NSString *file in allfiles) {
            NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
                [directoryList addObject:file];


        NSLog(@"array file name value ==== %@", directoryList);

        CCSprite *temp = [CCSprite spriteWithFile:[directoryList objectAtIndex:0]];
        [temp setTextureRect:CGRectMake(160.0f, 240.0f, 50,50)];
       // temp.anchorPoint = ccp(0,0);

        [self addChild:temp z:10];
        for(UIImage *file in directoryList) {

//             NSData *pngData = [NSData dataWithContentsOfFile:file];
//        image = [UIImage imageWithData:pngData];
        NSLog(@"uiimage = %@",image);
        //  UIImage *image = [UIImage imageWithContentsOfFile:file];
        for (int i=1; i<=3; i++) {
            for (int j=1;j<=3; j++) {
                CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:file] autorelease];

                CCSprite *selectedimage = [CCSprite spriteWithTexture:tex
                                                                 rect:CGRectMake(0, 0, 67, 66)];
                selectedimage.position = ccp(100*i,350*j);
                [self addChild:selectedimage];


    return self;
  • \$\begingroup\$ Useful question to me. I write a PNG file to the documents directory using CCRenderTexture, but then need to read it back. \$\endgroup\$
    – andrewz
    Feb 19, 2015 at 17:47

1 Answer 1


I was in same situation, after downloading image to my Documents directory, i have to use it in CCSprite. I used the function spriteWithCGImage, it worked for me. Here is my code,

    NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *logoImagePath = [NSString stringWithFormat:@"%@/%@", documentDirectory, @"image0.png"];
    UIImage *logoImage = [UIImage imageWithContentsOfFile:logoImagePath];
    // Then use spriteWithCGImage
    CCSprite *logoSprite = [CCSprite spriteWithCGImage:logoImage.CGImage key:nil];

Not the answer you're looking for? Browse other questions tagged .