UIImage screenshot loses its quality

前端 未结 5 1383
再見小時候
再見小時候 2020-12-11 09:37

I am merging two images and then I take a screenshot by applying this code:

UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGet         


        
相关标签:
5条回答
  • 2020-12-11 10:05

    i got the snapshot with good quality and particular location of screen. By this code.

    -(UIImage *)takeScreenShot
    {
        CGRect grabRect;
        grabRect = CGRectMake(0,70,320,260);
        UIGraphicsBeginImageContextWithOptions(grabRect.size, self.view.opaque, 0.0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
        [self.view.layer renderInContext:ctx];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return viewImage;
    }
    

    it gives me excellent snapshot..

    0 讨论(0)
  • 2020-12-11 10:08

    Try using the withOptions version of UIGraphicsBeginImageContext

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    
    0 讨论(0)
  • 2020-12-11 10:08

    Did you provide an image for retina display? You should check it. You might be running in a simulator (in retina).

    0 讨论(0)
  • 2020-12-11 10:14

    UIGraphicsBeginImageContextWithOptions(size, NO, 2.0); this solve my prblem by increasing scale from 1.0 to 2.0

    0 讨论(0)
  • 2020-12-11 10:19

    I've made a category on UIImage class that may help you. It goes like this:

    + (UIImage*)imageWithView:(UIView *)view opaque:(BOOL)opaque bgColor:(UIColor*)bgColor{
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, [[UIScreen mainScreen] scale]);
    
        if(!opaque){
            [bgColor set];
        }
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    

    It works fine for me. No bluring was detected. Try to use it. If you'll still have it then most likely the problem is in your saving code...

    Cheers... :)

    0 讨论(0)
提交回复
热议问题