UIImage screenshot loses its quality

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

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

[dragView removeFromSuperview];
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

The problem is that when the final image loses its quality it blurs.


Try using the withOptions version of UIGraphicsBeginImageContext

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

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();

    return viewImage;

it gives me excellent snapshot..

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]);

        [bgColor set];
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    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... :)

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

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

Need Your Help

Exif manipulation library for python

python exif

I'm looking for good exif (Exchangeable image file format) manipulation library for python. I prefer flexibility (e.g., ability to retrieve providers' proprietary tags) than processing speed. What ...

Can an eclipse plugin invoke an external python script?

java eclipse eclipse-plugin

My intention is to develop an eclipse plugin (I will explain why I am sticking on to eclipse plugin.), that will execute a python script at a specific location (say C:\MyProject\testscript.py).