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

时间:2010-05-18 12:56:55

标签: iphone screenshot landscape

我正在以横向模式运行应用。 我必须小心谨慎,以便拍摄的屏幕截图也处于横向模式。

我在iphone app中使用以下代码,底部有tabbar并进行屏幕截图,但它始终只在纵向模式下。为什么呢?

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

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

2 个答案:

答案 0 :(得分:2)

我之前的回答是不正确的。我已经研究过了这个问题,我得到了一个风景右侧屏幕截图的答案。这样可以将正确的横向屏幕保存到相机胶卷上。我没有试过景观。

这是另一个stackoverflow问题here中提供的答案的变体。这个答案的问题是屏幕截图总是有一个UIImageOrientationUp的imageOrientation(只读)。因此,我们应该知道方向是什么,并适当地改变它。

我希望这会有所帮助。如果这适用于横向左侧,或者您获得了适用于横向景观的变体,请发布。

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

答案 1 :(得分:1)

这是一个非常古老的主题,但是目前如果有人也遇到这个问题,现在该怎么做。

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

如果你想让它向右旋转,只需用UIImageOrientationRight替换UIImageOrientationLeft