Screen shot not provide image of whole screen

2019-05-26 22:26发布

问题:

I am making application related to images. I have multiple images on my screen. I had take screen shot of that. But it should not provide my whole screen. Little part of the top most & bottom most part need not be shown in that.

I have navigation bar on top. And some buttons at bottom. I don't want to capture that buttons and navigation bar in my screenshot image. Below is my code for screen shot.

-(UIImage *) screenshot
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

After taking screenshot I am using it by below code in facebook share method,

UIImage *image12 =[self screenshot];

[mySLComposerSheet addImage:image12]; 

回答1:

the easiest way to achieve this would be to add a UIView which holds all the content you want to take a screenshot of and then call drawViewHierarchyInRect from that UIView instead of the main UIView.

Something like this:

-(UIImage *) screenshot {
    UIGraphicsBeginImageContextWithOptions(contentView.bounds.size, YES, [UIScreen mainScreen].scale);

    [contentView drawViewHierarchyInRect:contentView.frame afterScreenUpdates:YES];

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Hope this helps!



回答2:

You can use my below code to take screen shot of a view.

I have put the condition to check the size of a screenshot.

With this code image is saved in your documents folder and from there you can use your image to share on Facebook or anywhere you want to share.

CGSize size = self.view.bounds.size;
    CGRect cropRect;

    if ([self isPad])
    {
        cropRect = CGRectMake(110 , 70 , 300 , 300);
    }

    else
    {
        if (IS_IPHONE_5)
        {
            cropRect = CGRectMake(55 , 25 , 173 , 152);
        }
        else
        {
            cropRect = CGRectMake(30 , 25 , 164 , 141);
        }
    }


    /* Get the entire on screen map as Image */
    UIGraphicsBeginImageContext(size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    /* Crop the desired region */
    CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
    UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    /* Save the cropped image
     UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);*/

    //save to document folder
    NSData * imageData = UIImageJPEGRepresentation(cropImage, 1.0);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    NSString *imagename=[NSString stringWithFormat:@"Pic.jpg"];

    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imagename];
    ////NSLog(@"full path %@",fullPathToFile);
    [imageData writeToFile:fullPathToFile atomically:NO];

Hope it helps you.



回答3:

use this code

   -(IBAction)captureScreen:(id)sender
    {
        UIGraphicsBeginImageContext(webview.frame.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    }

sample project www.cocoalibrary.blogspot.com

http://www.bobmccune.com/2011/09/08/screen-capture-in-ios-apps/



回答4:

snapshotViewAfterScreenUpdates

but it is only Available in iOS 7.0 and later.

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

This method captures the current visual contents of the screen from the render server and uses them to build a new snapshot view. You can use the returned snapshot view as a visual stand-in for the screen’s contents in your app. For example, you might use a snapshot view to facilitate a full screen animation. Because the content is captured from the already rendered content, this method reflects the current visual appearance of the screen and is not updated to reflect animations that are scheduled or in progress. However, this method is faster than trying to render the contents of the screen into a bitmap image yourself.

https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instm/UIScreen/snapshotViewAfterScreenUpdates: