我使用以下代码来获取屏幕截图并将其保存到相册中。
CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];
UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil);这很好用,但如果我的应用程序处于横向模式,则保存的图像不会正确显示。(需要90度转弯)。我需要做什么?
发布于 2010-07-27 09:28:13
测试以下任一项
[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等)。您可以将它们混合到一个函数中。
https://stackoverflow.com/questions/3340046
复制相似问题