6

サブビューとしていくつかのUIImageViewを持つUIViewがあります。これらのサブビューのそれぞれには、異なるアフィン変換が適用されています。UIViewのスクリーンキャプチャ、UIImageとしてのキャプチャ、またはその他の画像表現に相当するものを取得したいと思います。

私がすでに試した方法で、レイヤーをCGContextにレンダリングします。

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

サブビューのポジショニングやその他のアフィン変換は保持されません。

私は本当に正しい方向へのキックをいただければ幸いです。

4

2 に答える 2

11

これを試して:

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2009-09-15T00:48:11.680 に答える
0

Swift2.xのバージョンは次のとおりです。

// This flattens <allViews> into single UIImage
func flattenViews(allViews: [UIView]) -> UIImage? {
    // Return nil if <allViews> empty
    if (allViews.isEmpty) {
        return nil
    }

    // If here, compose image out of views in <allViews>
    // Create graphics context
    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, UIScreen.mainScreen().scale)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)

    // Draw each view into context
    for curView in allViews {
        curView.drawViewHierarchyInRect(curView.frame, afterScreenUpdates: false)
    }

    // Extract image & end context
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Return image
    return image
}
于 2016-03-31T23:31:26.127 に答える