Я использую следующий код, чтобы получить снимок экрана экрана и сохранить его к фотоальбомам.
CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];
UIImageWriteToSavedPhotosAlbum(screenimage, nil, nil), nil);
Это работает отлично, но если мое приложение находится в альбомном режиме, сохраненный образ не выходит право. (в потребностях поворот на 90 градусов). Что я должен сделать?
Проверьте на наличие того или другого
[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
и т.д.). Вы можете просто объединить их в одну функцию.