0

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

enter image description here

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


    }
Community
  • 1
  • 1
Sam B
  • 27,273
  • 15
  • 84
  • 121

1 Answers1

1

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

adali
  • 5,977
  • 2
  • 33
  • 40
  • that worked 50% - it took the screenshot of the image view section which is great but for some reason adding the label as subview didn't capture the label – Sam B Apr 19 '13 at 17:06
  • it will capture the label if the label is the subview of the imageview, unless you calculate the wrong position that the label is no shown in the visibal rect of the imageview – adali Apr 19 '13 at 17:08
  • you should recalculate the relative position from the label to the imageview first, until the label is displayed right to the image you provide above – adali Apr 19 '13 at 17:15
  • ok, this will take a lot of trail and error. To compound the problem I let users move label around. Now I have to figure out where did they leave the label relative to the image. Just adding this line doesn't do anything (in terms of screen capture) [testBgImgVw addSubview:customLabel1]; – Sam B Apr 19 '13 at 17:19
  • I guess my only hope is to crop the whole screen capture starting at certain X,Y coordinates in this case (0,80). – Sam B Apr 19 '13 at 17:19
  • i think you want to implement that add some text onto the imageview ? if i'm right , my way is correct, if you want to capture the label if label's position is out of the image , then use a canvasview to wrap all the controls – adali Apr 19 '13 at 17:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28523/discussion-between-sam-budda-and-adali) – Sam B Apr 19 '13 at 17:31