5

I am making an application using Box2D in which i am getting images from Asset Library and displaying them as sprites.

here is the code which i have done :

Getting Images from asset library :

CGImageRef imgRef = [[mutArrAssetPhotos objectAtIndex:i] thumbnail];

Creating Texture2D :

CCTexture2D *spriteTexture = [[CCTexture2D alloc]initWithCGImage:imgRef resolutionType:kCCResolutionUnknown];

Creating sprites from textures :

CCSprite *paddle = [CCSprite spriteWithTexture:spriteTexture];

This gives me warning in console like :

"cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha"

Still in simulator it works fine though warning but in device images are not being displayed.

But instead if i used :

CCSprite *paddle = [CCSprite spriteWithFile:@"img.png"];

it is working fine and is not giving any warning also.

Can anyone help please ?? Thanks in Advance.

Mansi Panchal
  • 2,357
  • 18
  • 27
  • I don't think "cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha" is really a warning, just a notice. It didn't find any alpha values and chooses another color format that will be faster to draw. You could have created your images in that color format, turning alpha OFF before saving. – Jonny Jun 05 '13 at 02:33

2 Answers2

3

I've solved it. Somehow i managed to recreate the image, from which i was creating sprite.

CGImageRef imgRef = [[mutArrAssetPhotos objectAtIndex:i] thumbnail];
        UIImage *image = [self updateImage:[UIImage imageWithCGImage:imgRef]];
        imgRef = image.CGImage;

        CCSprite *paddle = [CCSprite spriteWithCGImage:image.CGImage key:[NSString stringWithFormat:@"%d",i]];

And for updating the image, i redraw it so that i do not get any alpha warnings.The update method is like this :

-(UIImage *) updateImage:(UIImage *)image
{
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    imgView.frame = CGRectMake(0,50, image.size.width,image.size.height);

    image = [self captureImageFrom:imgView];

    [imgView release];
    return image;
}

-(UIImage *) captureImageFrom:(UIImageView *)view{

    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return screenShot;
}
Mansi Panchal
  • 2,357
  • 18
  • 27
0

I think (could not find docs to confirm tough) that the thumbnail changes the image format so you get a plain no alpha image ref.

Try using the following for creating the right image ref :

CGImageRef imgRef = [[[mutArrAssetPhotos objectAtIndex:i] defaultRepresentation] fullResolutionImage];
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • But i need thumbnail images only , because i've lots of images to be displayed at a time and i can't display them with full resolution. – Mansi Panchal Aug 28 '12 at 12:52
  • but did it work ? did you get those errors about the bits format ? If not than just scale the sprite to your desired size. – giorashc Aug 28 '12 at 13:06
  • No , I tried fullResolutionImage. But it didn't work. Even with Scaling it's showing the same warning for image has no alpha. – Mansi Panchal Aug 29 '12 at 04:34
  • It might be that your CCTexture2D is causing the problem. (btw I am not familiar with this texture init signature). Try creating the sprite using the imgRef object directly using spriteWithCGImage class method of CCSprite – giorashc Aug 29 '12 at 14:23
  • Tried that also.. But still no success. The main problem is that in simulator images are being displayed and working fine. But in iphone its not at all being displayed.. :( – Mansi Panchal Aug 30 '12 at 09:55