How to capture UIView to UIImage without loss of quality on retina display

前端 未结 17 1489
时光取名叫无心
时光取名叫无心 2020-11-22 00:42

My code works fine for normal devices but creates blurry images on retina devices.

Does anybody know a solution for my issue?

+ (UIImage *) imageWith         


        
17条回答
  •  醉话见心
    2020-11-22 01:06

    To improve answers by @Tommy and @Dima, use the following category to render UIView into UIImage with transparent background and without loss of quality. Working on iOS7. (Or just reuse that method in implementation, replacing self reference with your image)

    UIView+RenderViewToImage.h

    #import 
    
    @interface UIView (RenderToImage)
    
    - (UIImage *)imageByRenderingView;
    
    @end
    

    UIView+RenderViewToImage.m

    #import "UIView+RenderViewToImage.h"
    
    @implementation UIView (RenderViewToImage)
    
    - (UIImage *)imageByRenderingView
    {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
        UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snapshotImage;
    }
    
    @end
    

提交回复
热议问题