Taku
Taku

Reputation: 85

Photo orientation issue in Three20's TTPhotoViewController?

I'm currently having some troubles with TTPhotoViewController : I try to display some photos (taken from my iPhone 3GS camera), and the orientation of the displayed pictures are almost always erroneous ...

I mean that for example a photo taken in landscape mode will sometimes be displayed correctly, sometimes it will be upside down, sometimes it will be rotated ...

I've also noticed that a photo taken in portrait mode will be rotated (and so will take more than the whole screen), but rotating the iPhone will make it fit the screen well ...

I think I'm going mad :) any help would be greatly appreciated, thanks by advance.

EDIT : It seems it's a matter of size. I tried to downscale my picture and TTPhotoViewController doesn't screw up anymore, and then I try to rescale it to its initial size and the problem is occuring again. I can't understand that problem as a "memory limit" one, as my picture was taken with my iPhone ; moreover a UIImageView display it well ...

If anyone has a suggestion ...

Upvotes: 1

Views: 1333

Answers (2)

Guntis Treulands
Guntis Treulands

Reputation: 4762

for resizing both landscape and portrait images with correct orientation from camera roll and gallery I use this function:

-(UIImage *)resizeImage:(UIImage *)image 
{   
    CGImageRef imageRef = [image CGImage];
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();

    if (alphaInfo == kCGImageAlphaNone)
    {
        alphaInfo = kCGImageAlphaNoneSkipLast;
    }

    int width, height;

    width = 640;
    height = 480;

    CGContextRef bitmap;

    if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) 
    {
        bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);  
    } 
    else 
    {
        bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);  
    }

    if (image.imageOrientation == UIImageOrientationLeft) 
    {
        NSLog(@"image orientation left");
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -height); 
    } 
    else if (image.imageOrientation == UIImageOrientationRight) 
    {
        NSLog(@"image orientation right");
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -width, 0);  
    } 
    else if (image.imageOrientation == UIImageOrientationUp) 
    {
        NSLog(@"image orientation up");     
    } 
    else if (image.imageOrientation == UIImageOrientationDown) 
    {
        NSLog(@"image orientation down");   
        CGContextTranslateCTM (bitmap, width,height);
        CGContextRotateCTM (bitmap, radians(-180.));    
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
}

Upvotes: 2

ahmet emrah
ahmet emrah

Reputation: 1858

You probably set size: attribute of the photoSource to some wrong value.

Edit: Unfortunately i don't have another suggestion for your problem, however i have a warning. You should definitely scale your image before display, 1536x2048 is way too large for iphone to handle, and totally unnecessary for a 320x480 screen. Otherwise you are sure to have app crashes due to low memory in the future - if not now.

Upvotes: 1

Related Questions