屏幕截图不提供整个屏幕的图像

时间:2013-11-15 06:18:34

标签: ios iphone uiimageview screenshot

我正在制作与图像相关的应用程序。我的屏幕上有多个图像。我拍了那个屏幕截图。但它不应该提供我的整个屏幕。 最顶层的一小部分&最底层的部分不需要显示在那里。

我的导航栏位于顶部。底部还有一些按钮。我不想在我的截图图片中捕获那些按钮和导航栏。 下面是我的截屏代码。

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

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

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

截取屏幕截图后,我通过facebook分享方法中的以下代码使用它,

UIImage *image12 =[self screenshot];

[mySLComposerSheet addImage:image12]; 

4 个答案:

答案 0 :(得分:2)

实现这一目标的最简单方法是添加一个UIView,其中包含您想截取屏幕截图的所有内容,然后从该UIView而不是主UIView调用drawViewHierarchyInRect

这样的事情:

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

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

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

希望这有帮助!

答案 1 :(得分:0)

您可以使用我的以下代码拍摄视图的屏幕截图。

我已经设定条件来检查屏幕截图的大小。

使用此代码图像保存在您的文档文件夹中,然后您可以使用图像在Facebook或任何您想要共享的地方共享。

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

希望它对你有所帮助。

答案 2 :(得分:0)

使用此代码

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

示例项目www.cocoalibrary.blogspot.com

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

答案 3 :(得分:0)

snapshotViewAfterScreenUpdates

但仅适用于 iOS 7.0及更高版本

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

此方法从渲染服务器捕获屏幕的当前可视内容,并使用它们构建新的快照视图。您可以使用返回的快照视图作为应用程序中屏幕内容的可视替代。例如,您可以使用快照视图来促进全屏动画。由于内容是从已渲染的内容中捕获的,因此该方法反映了屏幕的当前视觉外观,并且未更新以反映已安排或正在进行的动画。但是,此方法比尝试自己将屏幕内容呈现为位图图像更快。

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

相关问题