Log in or register to post comments

Screenshot in portrait mode

October 19, 2012 - 12:46am #1

Hi,

Below code takes screenshot in landscpe mode correctly but when camera is in protrait then still it takes landscape photo. How can I make it work for both orientations.

 

UIImage *outputImage = nil;
    CGFloat scale = 1.0;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        scale = [[UIScreen mainScreen] scale];
    }
    VideoPlaybackAppDelegate *delegate = (VideoPlaybackAppDelegate *)[UIApplication sharedApplication].delegate;
    CGRect s;
    UIViewController *contr = (UIViewController *)delegate.arParentViewController;
    if (UIDeviceOrientationIsLandscape(contr.interfaceOrientation)) {
        s = CGRectMake(0, 0, 480.0f * scale, 320.0f * scale);
    }
    else if (UIDeviceOrientationIsPortrait(contr.interfaceOrientation)) {
        s = CGRectMake(0, 0, 320.0f * scale, 480.0f * scale);
    }
   
    uint8_t *buffer = (uint8_t *) malloc(s.size.width * s.size.height * 4);
   
    glReadPixels(0, 0, s.size.width, s.size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);   
   
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, buffer, s.size.width * s.size.height * 4, NULL);
   
    CGImageRef iref = CGImageCreate(s.size.width, s.size.height, 8, 32, s.size.width * 4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, ref, NULL, true, kCGRenderingIntentDefault);      
   
    size_t width = CGImageGetWidth(iref);   
    size_t height = CGImageGetHeight(iref);   
    size_t length = width * height * 4;           
    uint32_t *pixels = (uint32_t *)malloc(length);
   
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4,   
                                                 CGImageGetColorSpace(iref), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);          
   
    CGAffineTransform transform = CGAffineTransformIdentity;   
    transform = CGAffineTransformMakeTranslation(0.0f, height);   
    transform = CGAffineTransformScale(transform, 1.0, -1.0);   
    CGContextConcatCTM(context, transform);           
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);        
    CGImageRef outputRef = CGBitmapContextCreateImage(context);
   
    outputImage = [UIImage imageWithCGImage: outputRef];
   
    CGDataProviderRelease(ref);  
    CGImageRelease(iref);   
    CGContextRelease(context);   
    CGImageRelease(outputRef);
    free(pixels);
    free(buffer);
   
    return outputImage;

Screenshot in portrait mode

October 21, 2012 - 10:22pm #3

I just took the landscape photo and then rotate it 90 degress by ANImageBitmapRep library.

Screenshot in portrait mode

October 19, 2012 - 8:31am #2

Hi

Have a look at this link on the Apple developer web site that shows how to capture screenshots and also how to extend this to OpenGL ES buffers.

http://developer.apple.com/library/ios/#qa/qa1703/_index.html

HTH

N

Log in or register to post comments