简体   繁体   English

在风景视图中拍摄的iPhone屏幕

[英]iPhone screen shot in landscape view

I'm running the app in landscape mode. 我在横向模式下运行应用程序。 What care do I have to take so that the screen shot taken is also in landscape mode. 我必须小心谨慎,以便拍摄的屏幕截图也处于横向模式。

I'm using the following code in iphone app with tabbar at bottom and taking screen shot, but its always in portrait mode only. 我在iphone应用程序中使用以下代码,底部有tabbar并进行屏幕截图,但它始终只在纵向模式下。 why? 为什么?

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

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

My previous answer was incorrect. 我之前的回答是不正确的。 I have worked on this and I have an answer for a landscape right screen shot. 我已经研究过了这个问题,我得到了一个风景右侧屏幕截图的答案。 This will save a landscape right screen shot correctly to the camera roll. 这样可以将正确的横向屏幕保存到相机胶卷上。 I have not tried landscape left. 我没有尝试过景观。

This is a variation on an answer provided in another stackoverflow question here . 这是在另一个计算器问题提供了答案的变化在这里 The problem with that answer is that the screen shot always has an imageOrientation (read only) of UIImageOrientationUp. 这个答案的问题是屏幕截图总是有一个UIImageOrientationUp的imageOrientation(只读)。 Therefore it is up to us to know what the orientation was and change it appropriately. 因此,我们应该知道方向是什么,并适当地改变它。

I hope this helps. 我希望这有帮助。 If this works on landscape left or you get a variation that works on landscape left please post it. 如果这适用于左侧的景观,或者您获得了适用于景观的变体,请发布。

 - (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;
}

This is a really old thread, but here's how to do it currently in case anyone also has this issue. 这是一个非常古老的线程,但是如果有人也有这个问题,这里是如何做到这一点。

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];

If you want it rotated right, just replace UIImageOrientationLeft with UIImageOrientationRight 如果你想要它向右旋转,只需用UIImageOrientationRight替换UIImageOrientationLeft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM