如何拍摄视频控制器的快照而不在ios的屏幕中加载?

时间:2014-07-18 09:39:27

标签: ios objective-c ipad

在我的iPAD应用程序中,我有两个viewcontrollers,第一个viewcontroller有一个按钮,当我点击那个按钮时我想得到第二个viewcontroler的快照,而不在iPAD屏幕中加载第二个viewcontroller 。 如果我加载viewcontroler然后拍摄快照然后它正在工作但我的要求是做同样的事情而不在屏幕上加载viewcontroler。 请提供想法或链接或代码段。

3 个答案:

答案 0 :(得分:9)

试试这个: - 制作要拍摄屏幕截图的VC实例,然后在此方法中传递对象。

+ (UIImage *)renderImageFromView:(UIView *)view withRect:(CGRect)frame {
// Create a new context the size of the frame
UIGraphicsBeginImageContextWithOptions(frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Render the view
[view.layer renderInContext:context];
//[view drawRect:frame];

// Get the image from the context
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();

// Cleanup the context you created
UIGraphicsEndImageContext();

return renderedImage;
}

答案 1 :(得分:2)

此解决方案基于Dheeraj Kumar的答案,但如果您的视图也包含SpriteKit内容,这将有效。但它需要iOS7。

Swift中的代码,对于带有SpriteKit视图的ViewController,

{{template config_path="design/email/header"}}
{{inlinecss file="email-inline.css"}}

<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td>
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style='background: #fff' class="email-heading">
                        <h1 style='color: #68883e;'>Thank you for your order</h1>
                        <p>Your order is being process right now</p>
                    </td>

                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td class="order-details">
            <h3 style='display: inline; float: left'>Your order <span class="no-link">#{{var order.increment_id}}</span></h3>
            <p style='display: inline; float: right'>Placed on {{var order.getCreatedAtFormated('long')}}</p>
        </td>
    </tr>
    <tr class="order-information">
        <td>
            {{if order.getEmailCustomerNote()}}
            <table cellspacing="0" cellpadding="0" class="message-container">
                <tr>
                    <td>{{var order.getEmailCustomerNote()}}</td>
                </tr>
            </table>
            {{/if}}
            {{layout handle="sales_email_order_items" order=$order}}
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td class="address-details">
                        <h6>Bill to:</h6>
                        <p><span class="no-link">{{var order.getBillingAddress().format('html')}}</span></p>
                    </td>

                    <td class="address-details">
                        <h6>Client:</h6>
                        <p><span class="no-link">{{var order.getclientfirstname().format('html')}}</span></p>
                    </td>

                </tr>
                <tr>
                    {{depend order.getIsNotVirtual()}}
                    <td class="method-info">
                        <h6>Shipping method:</h6>
                        <p>{{var order.shipping_description}}</p>
                    </td>
                    {{/depend}}
                    <td class="method-info">
                        <h6>Payment method:</h6>
                        {{var payment_html}}
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table><!-- asd-->
{{layout handle="sales_email_order_items" order=$order}}
{{template config_path="design/email/footer"}}

不是问题的一部分,但您可以非常轻松地分享您的屏幕截图,

private func takeScreenshot() -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)
    view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

答案 2 :(得分:0)

只需实例化您的第二个视图控制器,然后截取屏幕截图,例如:

- (void)foo
{
   UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
   UIImage *screenShot = [self imageFromView:vc.view];
   // Do something with screenShot
}

- (UIImage *)imageFromView:(UIView *) view
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(view.frame.size);
    }
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}