iPhone横屏视图下的截图

4

我正在横屏模式下运行应用程序。 那么我需要注意什么,才能使截图也是横屏模式的呢?

我在 iPhone 应用程序中使用以下代码,并且底部有选项卡,但是截图总是只能在纵向模式下。为什么?

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

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

2
我的之前的回答是不正确的。我已经解决了这个问题,并且有一个适用于横向右侧截图的答案。这将正确地保存横向右侧截图到相机胶卷中。我还没有尝试过横向左侧。
这是另一个stackoverflow问题 here 中提供的答案的变体。那个答案的问题在于屏幕截图始终具有UIImageOrientationUp(只读)的图像方向。因此,我们需要知道方向并适当地更改它。
希望这可以帮助你。如果这适用于横向左侧或者你得到了适用于横向左侧的变化,请发布它。
 - (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
Satyman,这个更新的答案对你有用吗?还是你采取了另一种方法? - dredful

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即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接