iPhone iOS如何通过编程方式旋转截图?

4

我知道如何拍摄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)宽度和高度),以允许任何旋转而不会裁剪。

enter image description here


你想要旋转镜头的框架,以便你实际上拥有屏幕的对角线部分吗?还是你想在拍摄之前旋转屏幕? - jscs
实际上,我在屏幕上叠加了一个矩形来定义截图裁剪区域。这个矩形可以被用户旋转。我想要捕获由矩形框围起来的任何内容(该框可能与主视图呈角度)。 - Alex Stone
1个回答

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

希望能帮到您。

这基本上是我用来将大截屏裁剪成小截屏的代码。现在我需要弄清楚在哪里/如何进行旋转。 - Alex Stone

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