【问题标题】:iPhone screen shot in landscape view横向视图中的 iPhone 屏幕截图
【发布时间】:2010-05-18 12:56:54
【问题描述】:

我正在横向模式下运行应用程序。 我必须注意什么才能使拍摄的屏幕截图也处于横向模式。

我在 iphone 应用程序中使用以下代码,标签栏位于底部并进行屏幕截图,但它始终仅处于纵向模式。为什么?

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

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

【问题讨论】:

    标签: iphone screenshot landscape


    【解决方案1】:

    我之前的回答不正确。我已经解决了这个问题,并且我有一个横向右屏幕截图的答案。这会将横向右侧屏幕截图正确保存到相机胶卷中。我还没有尝试左横向。

    这是另一个 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;
    }
    

    【讨论】:

    • Satyman,这个更新的答案对你有用吗?还是你朝另一个方向走?
    【解决方案2】:

    这是一个非常古老的线程,但如果有人也有这个问题,这里是目前的方法。

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

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

    【讨论】:

    • 有用的答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多