2010-05-18 4 views
4

Je cours l'application en mode paysage. Quel soin dois-je prendre pour que la capture d'écran soit également en mode paysage. J'utilise le code suivant dans l'application iphone avec la barre d'onglets en bas et en prenant la capture d'écran, mais c'est toujours en mode portrait seulement. Pourquoi?Capture d'écran de l'iPhone en mode paysage

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; 

UIGraphicsBeginImageContext(screenWindow.frame.size); 
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Répondre

2

Ma précédente réponse était incorrecte. J'ai travaillé sur cela et j'ai une réponse pour un paysage en plein écran. Cela permettra d'enregistrer correctement une photo d'écran droite sur le rouleau de la caméra. Je n'ai pas essayé le paysage à gauche.

Il s'agit d'une variante d'une réponse fournie dans une autre question here de stackoverflow. Le problème avec cette réponse est que la capture d'écran a toujours une imageOrientation (lecture seule) de UIImageOrientationUp. Par conséquent, il nous appartient de savoir quelle était l'orientation et de la modifier de façon appropriée.

J'espère que cela aide. Si cela fonctionne sur le paysage à gauche ou vous obtenez une variation qui fonctionne sur le paysage gauche s'il vous plaît le poster.

- (IBAction)cameraButtonClick:(id)sender { 

    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; 

    UIGraphicsBeginImageContext(screenWindow.frame.size); 
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 


UIImageWriteToSavedPhotosAlbum([self scaleAndRotateImage:screenshot], nil, nil, nil); 

    UIGraphicsEndImageContext(); 
} 

// Code from: http://discussions.apple.com/thread.jspa?messageID=7949889 
- (UIImage *)scaleAndRotateImage:(UIImage *)image { 
    int kMaxResolution = 640; // Or whatever 

    CGImageRef imgRef = image.CGImage; 

    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 


    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > kMaxResolution || height > kMaxResolution) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = kMaxResolution; 
      bounds.size.height = roundf(bounds.size.width/ratio); 
     } 
     else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = roundf(bounds.size.height * ratio); 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
    CGFloat boundHeight; 

    boundHeight = bounds.size.height; 
    bounds.size.height = bounds.size.width; 
    bounds.size.width = boundHeight; 
    transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width/2.0); 
    transform = CGAffineTransformRotate(transform, M_PI/2.0); 

    UIGraphicsBeginImageContext(bounds.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIImageOrientation orient = image.imageOrientation; 

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } 
    else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 

    CGContextConcatCTM(context, transform); 

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return imageCopy; 
} 
+0

J'ai essayé aussi bien et cela n'a pas – Satyam

+1

Satyman, a fait ce travail de réponse mis à jour pour vous? ou êtes-vous allé dans une autre direction? – dredful

1

Ceci est un très vieux fil, mais voici comment le faire actuellement au cas où quelqu'un a aussi ce problème.

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 

CGRect rect = [keyWindow bounds]; 
UIGraphicsBeginImageContext(rect.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
[keyWindow.layer renderInContext:context]; 

UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return [[UIImage alloc] initWithCGImage:[image CGImage] scale:1 orientation:UIImageOrientationLeft]; 

Si vous voulez tourner à droite, il suffit de remplacer UIImageOrientationLeft avec UIImageOrientationRight

+0

Réponse utile.Merci. – Vignesh