Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

openGL ES "Texture2D" stretches images from photolibrary

NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
edited June 2009 in iPhone SDK Development
Hi,

wondering, if someone has ever experienced this issue. I'm using the Texture2D class to draw textures on the iPhone screen in openGL ES. This works just perfect, when using images, that were previously imported into XCode.

For example: I import a png texture (320x480 px) into XCode. Then I load the texture like:
_textures[kTexture_Background] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"myImportedTexture.png"]];

and draw it:
glDisable(GL_BLEND);
[_textures[kTexture_Background] drawInRect:[self bounds]];
glEnable(GL_BLEND);

The texture renders just perfect on the screen.

Now I use the UIImagePickerController to pick an image and show it using the same technique. The result is different now. The image gets stretched along the Y-axis and no longer fits the screen. X-axis on the other hand seems to be ok.

I thought, lets try to resize the image manually after picking, because it might be possible, that images in the photolibrary have a different scale.

So, I used the following method to resize the picked image:
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
	CGImageRef imageRef = [image CGImage];
	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
	CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
	
	if (alphaInfo == kCGImageAlphaNone)
		alphaInfo = kCGImageAlphaNoneSkipLast;
	
	CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
	UIImage *result = [UIImage imageWithCGImage:ref];
	
	CGContextRelease(bitmap);
	CGImageRelease(ref);
	
	return result;	
}

Even though I chose 320x480, I get the same stretched image, after passing it to the Texture2D class.

All I want, is to pick an image from the photolibrary and scale it to 320x480 pixel, then draw it, using openGL.

Any ideas, what might cause this? Thanks in advance.
Post edited by NewiPhoneDeveloper on
Websites:<br />
<a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
<a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
<br />
iPhone Apps: <br />
<a hr

Replies

  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited June 2009
    Well, I found out that it's not openGL's fault. There is something strange about images loaded from the photo library. Even though I tried to resize them using quartz, they still exceed the screen. There also seems to be a difference between photos synced from iTunes and those taken with the "home+on/off switch" screenshot function. Synced photos only get stretched slightly.

    Anyway, what confuses me most, is the fact that resizing them via quartz does not seem to work. I have to do more research.

    If anyone can suggest, it'd be greatly appreciated. Thanks
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
Sign In or Register to comment.