风景中的UIGetScreenImage()?

时间:2010-07-27 00:58:56

标签: iphone uiimage

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

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

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

1 个答案:

答案 0 :(得分:2)

测试

[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等)。您可以将它们混合成一个函数。