簡體   English   中英

截取部分屏幕截圖

[英]Taking screenshot of Part of Screen

我的視圖控制器中有一個UIImageView,需要抓取其screeshot。 上面也有一個UILabel。 UIImageView並不覆蓋整個頁面,只是一部分。 我該如何截取屏幕上一部分的屏幕截圖?

我敢打賭,以前曾問過這個問題,但我調查了這篇文章。 如何使用UIGraphicsBeginImageContextWithOptions? 它只不過與未定義的變量混淆

在此處輸入圖片說明

到目前為止,我所擁有的代碼已截取了整個內容的屏幕快照。 (不是我想要的)。 我嘗試使用screenRect無效。 我考慮過在拍攝整個屏幕截圖后裁剪圖片,但是問題是,每種設備的裁剪大小都會有所不同,例如iphone3,iphone4,iphone5,iPad 1,2、3等。

所以我的問題是,有一種方法可以截取一部分屏幕的屏幕截圖,而無需經過裁切路線嗎? 如果裁切是唯一的方法,那么有沒有辦法我可以可靠地剪切圖片而不必擔心具有高/低像素的其他iOS設備?

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


    }

好的,如果您只想捕獲圖像和圖像上的標簽(如果可以確保標簽始終在圖像上),則

在imageview上添加標簽(imageview addSubview:label)

然后將其渲染為新圖像

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

完成:)

如果您想拍攝幾個uiview(imageview,label等),最好的方法是提供一個UIView畫布

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

並添加您要在其上渲染的每個視圖,最后將其渲染為所需的新圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM