首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >景观中的UIGetScreenImage()?

问景观中的UIGetScreenImage()?
EN

Stack Overflow用户
提问于 2010-07-27 08:58:56
回答 1查看 855关注 0票数 1

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

代码语言:javascript
复制
 CGImageRef screen = UIGetScreenImage();
        UIImage *screenImage = [UIImage imageWithCGImage:screen];
    UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil);

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-27 09:28:13

测试以下任一项

代码语言:javascript
复制
[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight
[UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft
(and possibly more orientations)

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

这可以通过几种方式来完成,这种方式不需要太多的代码:

代码语言:javascript
复制
/// 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等)。您可以将它们混合到一个函数中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3340046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档