Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 捕捉iPhone屏幕_Ios_Objective C - Fatal编程技术网

Ios 捕捉iPhone屏幕

Ios 捕捉iPhone屏幕,ios,objective-c,Ios,Objective C,我使用以下代码来捕获屏幕。以前它工作正常,但现在它重新调整了nil图像。我正在使用iphone5、5c和5s + (UIImage *)screenshot { CGSize imageSize = CGSizeZero; imageSize = [UIScreen mainScreen].bounds.size; UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

我使用以下代码来捕获屏幕。以前它工作正常,但现在它重新调整了nil图像。我正在使用iphone5、5c和5s

+ (UIImage *)screenshot
{

        CGSize imageSize = CGSizeZero;

        imageSize = [UIScreen mainScreen].bounds.size;

        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        for (UIWindow *window in [[UIApplication sharedApplication] windows])
        {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, window.center.x, window.center.y);
            CGContextConcatCTM(context, window.transform);
            CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);

            if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
            {
                [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
            }
            else
            {
                [window.layer renderInContext:context];
            }
            CGContextRestoreGState(context);
        }

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return image;
}

嗯,这段代码对我有用。此代码捕获视图(
self.view
此处)及其所有子视图的屏幕截图。简而言之,就是屏幕截图

+ (UIImage *)screenshot {
   CGRectMake *captureFrame = [[UIScreen mainScreen] bounds]; //The portion and size of the area, you want to capture. I have set it to the size of window
   CALayer *layer;
   layer = self.view.layer;
   UIGraphicsBeginImageContext(self.view.bounds.size); 
   CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
   [layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return screenImage;
}

希望这有帮助。

这里有一个方法,我用它将
ui视图
捕获到
ui图像

+ (UIImage *)renderView:(UIView *)view toImageWithSize:(CGSize)size {
  UIGraphicsBeginImageContextWithOptions(size, NO, 0.);
  CGContextRef c = UIGraphicsGetCurrentContext();
  [view.layer renderInContext:c];
  UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return viewImage;
}
然后您可以使用
屏幕截图
方法,该方法使用上述方法将主窗口的
rootViewController
渲染为图像:

+ (UIImage *)screenshot {
  UIWindow *mainWindow = [UIApplication sharedApplication].windows.firstObject;
  UIView *mainView = mainWindow.rootViewController.view;
  return [self renderView:mainView toImageWithSize:mainView.bounds.size];
}

我希望这是有帮助的。

现在我正在使用这段代码,它工作得很好

+ (UIImage *)screenshot:(UIView *)view
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);
    [view.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

这给了我一个模糊的图像。我编辑了我的答案,调用
UIGraphicsBeginImageContextWithOptions
,而不是
UIGraphicsBeginImageContext
,以考虑屏幕的比例因子。这里的捕获框是什么?这是我要捕获的帧的大小。很抱歉,我没有提到。这也给了我模糊图像。您试图生成的图像大小是多少?请学习使用调试器,然后逐步检查代码,看看出了什么问题。之后,您将能够提出一个带有有用调试信息的适当问题。