截取屏幕部分的屏幕截图 [英] Taking screenshot of Part of Screen

查看:68
本文介绍了截取屏幕部分的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中有一个UIImageView,需要进行抓屏操作.上面也有一个UILabel.UIImageView并不覆盖整个页面,只是一部分.我该如何截取屏幕上一部分的屏幕截图?

I have a UIImageView in my view controller whose screeshot I need to take. There is a UILabel on it as well. UIImageView doesn't cover the whole page just part of it. How can I take screenshot of just section of the screen?

我敢打赌,之前曾有人问过这个问题,但是我看了这篇文章如何使用UIGraphicsBeginImageContextWithOptions?没什么,只是与未定义的变量混淆

I bet this question has been asked before but I looked into this post How to use UIGraphicsBeginImageContextWithOptions? and it was nothing but confusing with undefined variables

到目前为止,我拥有的代码截取了整个内容的屏幕快照.(不是我想要的).我尝试使用screenRect无效.我考虑过在拍摄整个屏幕截图后裁剪图片,但是问题是每种设备iphone3,iphone4,iphone5,iPad 1,2、3等的裁剪大小都会不同.

so far the code I have takes the screenshot of the whole thing. (not what I want). I tried playing with screenRect with no effect. I thought about cropping picture after taking the whole screen screenshot but the problem is that the crop sizes will be different for each device iphone3, iphone4, iphone5, iPad 1,2,3 etc.

所以我的问题是有一种方法可以截取屏幕一部分的屏幕截图,而无需经过裁切路线吗?如果只有裁剪是可行的方法,那么有没有办法我可以可靠地剪切图片而不必担心具有高/低像素的其他iOS设备?

So my question is there a way to take a screenshot of a section of screen without going through cropping route? If cropping is the only way to go then is there a way I can reliably cut the picture without worrying about different iOS devices with high/low pixels?

testBgImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 320, 220)];

....

    -(IBAction) takeScreenshot
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
        {
            //its iphone
            screenRect  = CGRectMake(0, 0, 320, 480);
        }
        else
        {
            //its pad
            screenRect  = CGRectMake(0, 0, 768, 1024);
        }


        // This code is for high resolution screenshot
        UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();


        NSData* imageData = UIImageJPEGRepresentation(viewImage, 1.0);
        NSString* incrementedImgStr = [NSString stringWithFormat: @"UserScreenShotPic.jpg"];

        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];

        // Now we get the full path to the file
        NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
        [imageData writeToFile:fullPathToFile2 atomically:NO];


    }

推荐答案

好的,如果您只想捕获图像&图像上的标签(如果可以确保标签始终在图像上),则

ok if you just only want to capture the image & the label on the image ( if you can make sure the label is always on the image), then

在imageview上添加标签(imageview addSubview:label)

add the label on the imageview (imageview addSubview:label)

然后将其渲染为新图像

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO,[[UIScreen mainScreen]scale]);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

完成:)

如果您想拍摄几个uiview(imageview,label等),最好的方法是提供一个UIView画布

if you wanna take a shot of several uiviews(imageview,label, etc), the best way is provide a UIView canvas

UIView *canvasView = [[UIView alloc]......]
[self.view addSubview:canvasView];

并添加您要在其上渲染的每个视图,最后将其渲染为所需的新图像

and add every view you wanna render on it , finally render it into a new image you want

这篇关于截取屏幕部分的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆