【问题标题】:iPhone iOS how to take a screenshot programmatically with rotation?iPhone iOS如何通过旋转以编程方式截取屏幕截图?
【发布时间】:2013-04-10 02:14:51
【问题描述】:

我知道如何截取 iPhone/iPad 屏幕的常规矩形屏幕截图(代码如下)。但是,我正在寻找一种方法来获取旋转任意度数的屏幕截图

如何使用任意旋转的矩形截取UIView的截图?

    //this is the rotation of the rectangle
        NSNumber* savedRotation = [NSNumber numberWithFloat: 0.35]; //radians, get the real number from a gesture recognizer

        //create a screenshot
        CGSize screenshotSize;

//screenshot size has to be adjusted for the rotation, or multiplied by the square root of 2
        if( UIDeviceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation]))
        {
            screenshotSize  =  CGSizeMake(1024,768);
        }else
        {
            screenshotSize  =  CGSizeMake(768, 1024);

        }

        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(screenshotSize, NO, [UIScreen mainScreen].scale);


        else
            UIGraphicsBeginImageContext(screenshotSize);

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);

    //this does the proper rotation, but there's some kind of extra translation required to make it capture what's under the rectangle
        CGContextRotateCTM(context, -savedRotation.floatValue);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];


        UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

        CGContextRestoreGState(context);

这是上面代码的结果,它似乎部分工作,但需要对 CTM 进行一些额外的翻译才能完美匹配捕获矩形。此外,我需要将上下文的边界更改为更宽(可能是 1024 * sqrt(2) 宽度和高度),以允许任何旋转而不进行裁剪。

【问题讨论】:

  • 您想要旋转镜头的框架,这样您就可以拥有屏幕的对角线部分?或者你想让屏幕在你拍摄之前旋转?
  • 实际上我在屏幕上覆盖了一个矩形,用于定义屏幕截图裁剪区域。这个矩形可以由用户旋转。我希望捕捉矩形框架所包含的任何内容(可以与主视图相关的角度)

标签: objective-c ipad uiview ios6 screenshot


【解决方案1】:

以下是根据简单矩形裁剪图像所需的代码。

我不确定您如何跟踪矩形的旋转,但从那里应用此解决方案应该非常简单;

// Create rectangle from middle of current image
CGRect croprect = CGRectMake(image.size.width / 4, image.size.height / 4 ,
(image.size.width / 2), (image.size.height / 2));

// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect);

// Create new cropped UIImage
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

希望对你有帮助。

【讨论】:

  • 这几乎是我用来将较大的屏幕截图裁剪成较小屏幕截图的代码。现在我需要弄清楚在哪里/如何进行轮换
猜你喜欢
  • 2011-04-06
  • 2016-03-18
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多