【问题标题】:Capture UIImage of UIView including UINavigationBar捕获 UIView 的 UIImage 包括 UINavigationBar
【发布时间】:2012-02-16 03:53:26
【问题描述】:

我发现这段代码非常好:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

但我还需要捕获 UINavigationBar,因为我想在我的 UIStoryboardSegue 中使用图像作为过渡层。有什么想法吗?

【问题讨论】:

    标签: ios animation uiview uiimage uinavigationbar


    【解决方案1】:

    使用视图的窗口作为视图,这样导航栏和状态栏也将包括在内。如果您需要移除状态栏,则必须裁剪图像。

    【讨论】:

    • 如何在推送之前截取包含导航栏的视图控制器?我需要全屏截屏目标视图控制器。
    • 如果你有一个指向视图控制器的指针,你可以很容易地访问该视图控制器的视图,即使它还没有被推送。
    • 是的,但是 1. 导航栏不属于视图和 2. 在它被推送之前我不能访问它的 NavigationBar。 VC2 中的导航栏外观与 VC1 不同。
    • 如果你想包含导航栏或导航控制器的视图,你必须访问视图的窗口。如果它没有被推送,并且你想包含导航栏,你必须在截图之前推送它。
    【解决方案2】:

    你应该截取 self.navigationController?.view 的截图。 这样您就可以使用导航栏获得整个视图,但没有状态栏。

    if let view = self.navigationController?.view {
        let snapshot = view.snapshotViewAfterScreenUpdates(false)
    }
    

    【讨论】:

      【解决方案3】:

      而不是 view.layer 提供 appdelegate.window.layer 的引用它会起作用。

      【讨论】:

        【解决方案4】:

        哇,这些答案都不起作用。这是获取包括导航控制器在内的所有内容的屏幕截图的方法

        -(void) takeScreenshot
        {
        
            CGRect screenRect = [[UIScreen mainScreen] bounds];
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
            {
                //its iphone
        
        
                CGSize result = [[UIScreen mainScreen] bounds].size;
                if(result.height == 480 || result.width == 480)
                {
                    // iPhone Classic
                    screenRect  = CGRectMake(0, 0, 320, 480);
                }
                if(result.height == 568 || result.width == 568)
                {
                    // iPhone 5
                    screenRect  = CGRectMake(0, 0, 320, 568);
                }
            }
            else
            {
                //its pad
                screenRect  = CGRectMake(0, 0, 768, 1024);
            }
        
            NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString* documentsDirectory = [paths objectAtIndex:0];
        
            // This code is for high resolution screenshot
            UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
        
            //this was pre iOS 7.0
            //[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        
            // iOS 7.x -->
            [[[[UIApplication sharedApplication] windows] objectAtIndex:0] drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; // Set To YES
        
            UIImage *viewImage2 = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
        
            NSData* imageData2 = UIImageJPEGRepresentation(viewImage2, 1.0);
            NSString* incrementedImgStr2 = [NSString stringWithFormat: @"UserScreenShotPic.jpg"];
        
        
            // Now we get the full path to the file
            NSString* fullPathToFile3 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr2];
            [imageData2 writeToFile:fullPathToFile3 atomically:NO];
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多