문제

여러 uiimageviews를 서브 뷰로 가지고있는 uiview가 있습니다. 이러한 각 하위 뷰에는 다른 아핀 변환이 적용되었습니다. 나는 Uiview의 화면 캡처, UIIMAGE 또는 다른 이미지 표현으로 캡처하는 금액을 가져 가고 싶습니다.

내가 이미 시도한 방법은 다음과 같이 cgcontext로 레이어를 렌더링합니다.

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

내 하위 뷰의 포지셔닝 또는 기타 아핀 변환을 보존하지 않습니다.

나는 올바른 방향으로 차기에 감사드립니다.

도움이 되었습니까?

해결책

이 시도:

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

다른 팁

다음은 Swift 2.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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top