少なくとも iOS 7 をサポートしている場合、現在受け入れられている回答は古くなっています。
iOS7+ のみをサポートしている場合は、以下を使用する必要があります。
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
スウィフト 4:
func imageWithView(view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
return UIGraphicsGetImageFromCurrentImageContext()
}
この記事によると、iOS7 の新しい方法drawViewHierarchyInRect:afterScreenUpdates:
は より何倍も高速であることがわかりますrenderInContext:
。