0

I am able to take screenshot from UIView at iPhone 6 with below code: -

Here I have printed 2 logs. First gives me image size {375, 667} but after converting it to PNG format it gives me image size {750, 1334}.

I know that it's converting it for the retina display. Each point is represented by 2 pixels on the screen so the saved image is double the resolution.

But I need image size {375, 667} with PNG format.

- (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSLog(@"Image Size: %@", image);  // Image Size: <UIImage: 0x1700880c0>, {375, 667}

    NSData* imageData =  UIImagePNGRepresentation(image);
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);

    NSLog(@"PNG Image Size: %@", pngImage);  // PNG Image Size: <UIImage: 0x174087760>, {750, 1334}

    return image;
}


Big Image:- enter image description here

Small Image:-

enter image description here

Asif Raza
  • 836
  • 12
  • 29

2 Answers2

1

just to not get a bad quality you can create an image with screen scale

but when you load you can use this

CGFloat screenScale = [UIScreen mainScreen].scale;
if (screenScale != img.scale) {
    img = [UIImage imageWithCGImage:img.CGImage scale:screenScale    orientation:img.imageOrientation];
}
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
  • Thanks @Abdelahad Darwish. It gives me Image Size: {375, 667} in NSLog but when I am saving it using UIImageWriteToSavedPhotosAlbum then it again gives me image dimension {750, 1334} in Photos(Camera Roll). Please help. I need Image Size: {375, 667} in PNG with good quality of image. – Asif Raza Aug 21 '17 at 06:45
  • see your code you can compress image with this ` UIImage* pngImage = [UIImage imageWithData:imageData scale:1];` – Abdelahad Darwish Aug 21 '17 at 06:48
  • I tried UIImage* pngImage = [UIImage imageWithData:imageData scale:1]; but it gives same image dimension {750, 1334} in Photos(Camera Roll). – Asif Raza Aug 21 '17 at 07:02
0

Change the scale to 1 instead of [UIScreen mainScreen].scale.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
Imad Ali
  • 3,261
  • 1
  • 25
  • 33
  • I have also checked with this it gives me PNG image size {375, 667} but problem is that it reduces image quality. Image got blurred. I need PNG image as same size as JPG with same quality. Please help. – Asif Raza Aug 21 '17 at 05:03
  • I have attached 2 images. Big with [UIScreen mainScreen].scale and small is with 1. Here I am getting quality issue in small image. – Asif Raza Aug 21 '17 at 05:12
  • How can I fix it also? Please help, thanks in advance. – Asif Raza Aug 21 '17 at 05:18
  • @AsifRaza Keep scale to original, Try this, use this method: https://stackoverflow.com/a/613380/1025063 – Imad Ali Aug 21 '17 at 05:26