Capture iOS «Capture d'écran» du contrôleur de la caméra
-
14-11-2019 - |
Question
Dans mon application, j'affiche l'appareil photo et je prends des captures d'écran de certains Perts en utilisant UigetsCreenImage, (j'ai essayé UigraphicsGetImagefromCurrentimageContext et cela fonctionne très bien pour des captures d'écran sur presque n'importe quelle partie de mon application, mais pour la vue de la caméra, elle ne rendra pas une image blanche vierge). .. Quoi qu'il en soit, je crains qu'Apple rejette mon application à cause de UigetsCreenImage ... comment puis-je prendre une "capture d'écran" d'une boîte de 50px par 50px du coin supérieur gauche de la caméra sans utiliser cette méthode? J'ai cherché et tout ce que je pouvais trouver était "Avcaptures" et je ne pouvais pas trouver grand-chose sur ce que cela fait, ou si c'est même ce que je recherche ... une idée? :) Merci les gars!!!
La solution
Il ne devient pas beaucoup plus clair que les documents d'Apple sur Comment capturer la vue de la caméra. Oui, cela implique la classe AVCaptureSession
.
Si vous avez réellement besoin d'une capture d'écran de l'interface, vous devriez prendre au Docs pour ça. Code de coupe et de coller du lien (si cela ne fonctionne pas, vous devez soumettre un rapport de bogue à Apple):
Mise à jour: Il semble que cette approche ne soit plus prise en charge sur les versions plus récentes d'iOS. Le deuxième lien est maintenant également rompu.
- (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;
}
Autres conseils
Depuis iOS7, vous pouvez utiliser:
drawviewhierarchyinrect
UIImage *image;
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();