简体   繁体   中英

Taking screenshot of Part of Screen

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?

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

在此处输入图片说明

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.

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

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

then render it into a new image

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

done :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM