【问题标题】:Screen shot not provide image of whole screen屏幕截图不提供整个屏幕的图像
【发布时间】:2013-11-15 06:18:34
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios iphone uiimageview screenshot


    【解决方案1】:

    实现此目的的最简单方法是添加一个 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;
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我尝试使用 uiimageview 而不是 UIView,但它s doesnt help me.lets 尝试使用您的代码。
    【解决方案2】:

    您可以使用我下面的代码截取视图。

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

    使用此代码图像保存在您的文档文件夹中,您可以从那里使用您的图像在 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];
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      使用此代码

         -(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/

      【讨论】:

        【解决方案4】:

        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:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-12
          • 2021-04-16
          相关资源
          最近更新 更多