UIGetScreenImage() 在横向?

发布于 2024-09-11 16:32:55 字数 292 浏览 2 评论 0原文

我使用以下代码来获取屏幕截图并将其保存到相册中。

 CGImageRef screen = UIGetScreenImage();
        UIImage *screenImage = [UIImage imageWithCGImage:screen];
    UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil);

这很好用,但如果我的应用程序处于横向模式,则保存的图像不会正确显示。 (需要 90 度转弯)。我需要做什么?

I use the following code to get a screenshot of the screen and save it to the photo albums.

 CGImageRef screen = UIGetScreenImage();
        UIImage *screenImage = [UIImage imageWithCGImage:screen];
    UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil);

This works great but if my app is in landscape mode the saved image does not come out right. (in needs a 90 degree turn). What do i need to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-09-18 16:32:55

测试其中之一

[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight
[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft
(and possibly more orientations)

然后对图像进行适当的转换。

这可以通过多种方式完成,这个不需要很多代码:

/// click action saves orientation & picture:
-(void)screenshot
{
    self.orientationWhenPicked = [UIDevice currentDevice].orientation;
    CGImageRef screen = UIGetScreenImage();
    self.image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);
}

/// somewhere else we are called for the image
-(UIImage*)getScreenshot
{
    UIDeviceOrientation useOrientation = self.orientationWhenPicked;
    UIImage *img = self.image;

    UIGraphicsBeginImageContext(CGSizeMake(480.0f, 320.0f));
    CGContextRef context = UIGraphicsGetCurrentContext();
    if ( useOrientation == UIDeviceOrientationLandscapeRight )
    {
            CGContextRotateCTM (context, M_PI/2.0f);
            [img drawAtPoint:CGPointMake(0.0f, -480.0f)];
    } else {
            CGContextRotateCTM (context, -M_PI/2.0f);
            [img drawAtPoint:CGPointMake(-320.0f, 0.0f)];
    }
    UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return ret;
}

我在发布之前对其进行了一些修改以使其独立,但它应该需要一点努力(例如创建 self.image 等)。您可以将它们混合到一个函数中。

Test for either

[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight
[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft
(and possibly more orientations)

Then do the appropriate transformations to the image.

This can be done in several ways, this one doesn't take a lot of code:

/// click action saves orientation & picture:
-(void)screenshot
{
    self.orientationWhenPicked = [UIDevice currentDevice].orientation;
    CGImageRef screen = UIGetScreenImage();
    self.image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);
}

/// somewhere else we are called for the image
-(UIImage*)getScreenshot
{
    UIDeviceOrientation useOrientation = self.orientationWhenPicked;
    UIImage *img = self.image;

    UIGraphicsBeginImageContext(CGSizeMake(480.0f, 320.0f));
    CGContextRef context = UIGraphicsGetCurrentContext();
    if ( useOrientation == UIDeviceOrientationLandscapeRight )
    {
            CGContextRotateCTM (context, M_PI/2.0f);
            [img drawAtPoint:CGPointMake(0.0f, -480.0f)];
    } else {
            CGContextRotateCTM (context, -M_PI/2.0f);
            [img drawAtPoint:CGPointMake(-320.0f, 0.0f)];
    }
    UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return ret;
}

I modified it a little before posting to make it self-contained, but it should work with a little effort (e.g. create self.image etc). You may just blend them into one function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文