核心代碼
view生成圖片方法
UIGraphicsBeginImageContext(view.bounds.size); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
生成的清晰圖片
#pragma mark 生成image - (UIImage *)GetmakeImageWithView:(UIView *)view andWithSize:(CGSize)size { // 下面方法,第一個參數表示區域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數就是屏幕密度了,關鍵就是第三個參數 [UIScreen mainScreen].scale。 UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }