iOS/Swift + Objective-c

[Swift / Obejctive-c] UIView를 UIImage로 변환하기 (render UIView to UIImage)

안경 쓴 귀니 2022. 6. 14. 19:23
반응형

Swift, Objective-C에서 UIImage를 UIImage로 변환하는 방법 (render UIView to UIImage)

 

  • Swift
func viewToImage(view: UIView) -> UIImage {
    let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
    let image = renderer.image { context in
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    }
    return image
}

 

  • Objective-c
- (UIImage*)viewToImage:(UIView*)view {
    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size];
    UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    }];
    return image;
}

 

반응형