iPhone:如何通过编程创建屏幕截图并将其添加为子视图

3

我想调用一个可以截屏并将截屏加载为子视图的方法。我正在使用苹果提供的示例代码来进行截屏(如下所示),并尝试在我的代码中使用结果(一张图片)。然而,我不知道如何将图片从该方法传递到我的代码中。以下是我尝试过的方法,显然是错误的,但这是我能想到的全部:

    // Test Screenshot:

screenShot = [UIImage screenshot]; // THIS DOESN'T WORK
screenShotView = [[UIImageView alloc] initWithImage:screenShot];
[screenShotView setFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:screenShotView];

以下是苹果公司提供的该方法的示例代码:

 - (UIImage*)screenshot 
{
    NSLog(@"Shot");

    // 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;
}

任何帮助都将不胜感激!谢谢。

编辑:这是viewDidLoad方法,我在其中创建一个TextView,然后尝试捕获它的屏幕截图:

    - (void)viewDidLoad {


    // Setup TextView:

    NSString* someText = @"Some Text";
    CGRect frameText = CGRectMake(0, 0, 320, 480); 
    aTextView = [[UITextView alloc] initWithFrame:frameText];
    aTextView.text = someText;  
[self.view addSubview:aTextView];


    // Test Screenshot:

    screenShotView = [[UIImageView alloc] initWithImage:[self screenshot]];
    [screenShotView setFrame:CGRectMake(10, 10, 200, 200)];
    [self.view addSubview:screenShotView];

    [self.view bringSubviewToFront:screenShotView];

    [super viewDidLoad];



}
1个回答

3

要使用该图片,只需更改此行代码

screenShot = [UIImage screenshot];

To

screenShot = [self screenshot];

编辑:检查[self screenshot]是否返回有效的图像或nil。

   - (void)viewDidLoad {


    // Setup TextView:

    NSString* someText = @"Some Text";
    CGRect frameText = CGRectMake(0, 0, 320, 480); 
    aTextView = [[UITextView alloc] initWithFrame:frameText];
    aTextView.text = someText;  
    [self.view addSubview:aTextView];


    // Test Screenshot:

    UIImage *screenShotImage = [self screenShot];
    if(screenShot){
            screenShotView = [[UIImageView alloc] initWithImage:screenShotImage];
            [screenShotView setFrame:CGRectMake(10, 10, 200, 200)];
            [self.view addSubview:screenShotView];

            [self.view bringSubviewToFront:screenShotView];
    }else
         NSLog(@"Something went wrong in screenShot method, the image is nil");

    [super viewDidLoad];



}

这取决于您如何调用一个包含以下代码的方法:screenShot = [UIImage screenshot]; // THIS DOESN'T WORK screenShotView = [[UIImageView alloc] initWithImage:screenShot]; [screenShotView setFrame:CGRectMake(0, 0, 320, 480)]; [self.view addSubview:screenShotView]; 请始终粘贴整个方法,因为很难知道您到底在做什么。 - Cyprian
@Cyprian:我更新了上面的代码,并包含了整个viewDidLoad方法,我在其中尝试捕获屏幕。非常感谢你的帮助! - n.evermind
@n.evermind 目前不知道出了什么问题,但请看我的编辑,我添加了一个验证图像并检查其是否为空的代码。这意味着在截屏方法中出现了问题。 - Cyprian
@Cyprian:方法screenshot(不是大写S-对于这个混淆我很抱歉)显然返回有效的图像。我没有收到NSLog消息。所以我猜我尝试显示截图时出了问题。将尝试将其保存到photoLibrary,也许这将显示是否实际拍摄了屏幕截图。再次感谢您的帮助! - n.evermind
1
只是一个直觉:问题可能是你在viewDidLoad中设置文本、捕获图像和显示图像的顺序有问题。也许在捕获屏幕截图之前,视图没有更新其显示。你可以通过为每个步骤创建单独的IBAction,并通过按钮操作调用它们来测试这一点。或者在捕获屏幕截图之前和应用屏幕截图之后都调用setNeedsDisplay方法来刷新视图。 - Wienke
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接