Loading pngs
ImageIO was introduced with 10.4.
AnotherJake Wrote:For something that works on both iPhone and Mac to load a PNG, you can do something like this (I just kind of threw it together real quick, so it's pseudocode-ish, but you should be able to figure it out from here):
I don't know if this works on 10.4 though. I don't target 10.4 anymore since I've seen too much anecdotal evidence that few use it anymore.Code:
NSURL *url = [NSURL fileURLWithPath:myPath];
if (url == NULL)
{
NSLog(@"%s ERROR: Unable to create URL", __FUNCTION__);
return;
}
CGDataProviderRef source = CGDataProviderCreateWithURL((CFURLRef)url);
if (source == NULL)
{
printf("%s ERROR: Unable to create data provider from file URL", __FUNCTION__);
return;
}
CGImageRef image = CGImageCreateWithPNGDataProvider(source, nil, NO, kCGRenderingIntentDefault);
if(image == NULL)
{
NSLog(@"%s ERROR: unable to load: %@.%@", __FUNCTION__, texture[texID].file, extension);
CGDataProviderRelease(source);
return;
}
CGSize imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
NSUInteger width = imageSize.width;
NSUInteger height = imageSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *pixels = calloc(width * height * 4, 1);
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
Thanks a lot. The code works except that it's drawing everything rotated 180 degrees, upside down and backwards, is there some way to flip the context or image?
NelsonMandella Wrote:Thanks a lot. The code works except that it's drawing everything rotated 180 degrees, upside down and backwards, is there some way to flip the context or image?
Picky!

The flipY stuff is the only mod you should need:
Code:
colorSpace = CGColorSpaceCreateDeviceRGB();
pixels = calloc(width * height * 4, 1);
context = CGBitmapContextCreate(pixels, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
if (flipY)
{
CGContextTranslateCTM(context, 0, (float)(height));
CGContextScaleCTM(context, 1.0, -1.0);
}
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
Perfect! Thanks so much for the help.
I have switched to pnglite for my PNG loading problems. Much less trouble than libpng IMHO.
http://www.karlings.com/~danne/pnglite/
http://www.karlings.com/~danne/pnglite/
Ingemar Wrote:I have switched to pnglite for my PNG loading problems. Much less trouble than libpng IMHO.
http://www.karlings.com/~danne/pnglite/
Looks nice. Does it work on iPhone too?
AnotherJake Wrote:Looks nice. Does it work on iPhone too?I havn't tried that myself. But I wouldn't expect that to be a problem.

