В моем приложении я отображаю камеру и делаю скриншоты определенных персон, используя UIGetScreenImage (я пробовал UIGraphicsGetImageFromCurrentImageContext, и он отлично работает для скриншотов практически в любой части моего приложения, но для просмотра с камеры он просто возвращает пустое белое изображение). .. В любом случае, я боюсь, что Apple отклонит мое приложение из-за UIGetScreenImage... Как я могу сделать «скриншот» окна размером 50 на 50 пикселей из верхнего левого угла камеры, не используя этот метод? Я искал, и все, что я мог найти, было «AVCaptureSession», и я не мог найти много о том, что это делает, или даже то, что я ищу ... Есть понимание? :) Спасибо, парни!!!
iOS Захват скриншота контроллера камеры
Ответы (2)
Это не намного яснее, чем документация Apple о том, как захватывать изображение с камеры. просмотреть. Да, это касается класса AVCaptureSession
.
Если вам действительно нужен снимок экрана интерфейса, сделайте его на документы для этого. Вырежьте и вставьте код из ссылки (если это не сработает, отправьте отчет об ошибке в Apple):
Обновление. Похоже, этот подход больше не поддерживается в новых версиях iOS. Вторая ссылка тоже битая.
- (UIImage*)screenshot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
person
Community
schedule
25.01.2012
@DummyCode: я использую тот же метод, но он не захватывает предварительный просмотр камеры.
- person TamilKing; 06.11.2013
который захватил текущее изображение экрана, а не слой предварительного просмотра камеры
- person famfamfam; 30.10.2020
Начиная с iOS7 вы можете использовать:
DrawViewHierarchyInRect
UIImage *image;
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
person
shannoga
schedule
16.11.2014
Он рисует все (даже данные OpenGL) в UIImage, кроме предварительного просмотра камеры. iOS 8.1
- person vir us; 24.02.2015
Спасибо, мне нужно было захватить также камеру и OpenGL, и это сработало как шарм!
- person ibm123; 24.03.2020