gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:12:04 25 4
gpt4 key购买 nike

我想调用一个截取屏幕截图的方法,然后将此屏幕截图加载为 subview 。我正在使用 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];

这是 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,然后 try catch 它的屏幕截图:

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



}

最佳答案

要使用图像只需更改这一行

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



}

关于iPhone:以编程方式创建屏幕截图,然后将其添加为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5760160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com