iPhone:以编程方式创建屏幕截图,然后将其添加为子视图

发布于 2024-11-02 19:27:47 字数 3138 浏览 1 评论 0原文

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

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

这是Apple的该方法的示例代码:

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



}

I would like to call a method which takes a screenshot and then load this screenshot as a subview. I am using Apples sample code on how to take a screen shot (see below) and was trying to use the result (an image) in my code. However, I don't really know how to get the image from the method into my code. This is what I tried; it's obviously wrong, but it's all I could come up with:

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

And this is Apple's sample code for the method:

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

Any help would be very much appreciated! Thanks.


EDIT: This is the viewDidLoad method in which I create a TextView and then try to capture a screen shot of it:

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



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-11-09 19:27:47

要使用图像,只需更改此行

screenShot = [UIImage screenshot];

即可

screenShot = [self screenshot];

编辑:检查 [self Screenshot] 是否返回有效图像或为零。

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



}

To use the image just change this line

screenShot = [UIImage screenshot];

To

screenShot = [self screenshot];

Edit: Check to see if the [self screenshot] returns a valid image or 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];



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文