Screen shot not provide image of whole screen

帅比萌擦擦* 提交于 2019-12-06 10:57:14
gabriellanata

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!

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.

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/

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!