【问题标题】:iPhone: Create a screenshot programmatically and then add it as a subviewiPhone:以编程方式创建屏幕截图,然后将其添加为子视图
【发布时间】:2011-08-11 05:40:24
【问题描述】:

我想调用一个截取屏幕截图的方法,然后将此屏幕截图加载为子视图。我正在使用 Apple 的示例代码来了解如何截屏(见下文),并尝试在我的代码中使用结果(图像)。但是,我真的不知道如何将图像从方法中获取到我的代码中。这是我尝试过的;这显然是错误的,但这是我能想到的:

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

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


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

    - (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];



}

【问题讨论】:

    标签: iphone objective-c cocoa-touch methods


    【解决方案1】:

    要使用图像只需更改此行

    screenShot = [UIImage screenshot];
    

    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:我更新了上面的代码并包含了我尝试捕获屏幕的整个 viewDidLoad 方法。非常感谢您的帮助!
    • @n.evermind 到目前为止还不知道出了什么问题,但是看看我的编辑,我添加了一个代码来验证图像并检查它是否为零。这意味着截屏的方法有问题。
    • 只是一种预感:问题可能在于您正在设置文本、捕获图像并在 viewDidLoad 中显示所有内容吗?在您捕获屏幕截图之前,视图可能没有更新其显示。您可以通过为每个步骤创建一个单独的 IBAction 并通过按钮操作调用它们来测试这一点。和/或在视图上调用 setNeedsDisplay,在捕获屏幕截图之前和应用它之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多