0

I have reviewed and applied many reference to resize image with same quality but not able to get.

I am able to get image from UIView and resize with code as mentioned below: -

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

    NSData* imageData =  UIImagePNGRepresentation(image);
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil); // PNG image size: {750, 1334}
    NSLog(@"PNG Image Size: %@", pngImage);


    CGSize size = CGSizeMake(375, 667);
    [self imageWithImage1:pngImage scaledToSize:size];
    [self imageWithImage2:pngImage scaledToSize:size];

    return image;
}

- (UIImage *)imageWithImage1:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData* imageData1 =  UIImagePNGRepresentation(newImage);
    UIImage* scaledImage1 = [UIImage imageWithData:imageData1];
    UIImageWriteToSavedPhotosAlbum(scaledImage1, nil, nil, nil); // PNG Resized image 1 size: {375, 667}
    NSLog(@"PNG Scaled Image Size: %@", scaledImage1);

    return newImage;
}

-(UIImage *)imageWithImage2:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a bitmap context.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData* imageData1 =  UIImagePNGRepresentation(newImage);
    UIImage* scaledImage1 = [UIImage imageWithData:imageData1];
    UIImageWriteToSavedPhotosAlbum(scaledImage1, nil, nil, nil); // PNG Resized image 2 size: {375, 667}
    NSLog(@"PNG Scaled Image Size: %@", scaledImage1);

    return newImage;
}

PNG image size: {750, 1334}

Both image I am getting from PNG image after resizing is {375, 667}. Which is correct as per my requirement.

But the quality of both resized image is down from original PNG image. I can recognise that button, icons and texts are blurry.

How can I fix it. Please help, thanks in advance.

I need resized image with same the quality as PNG image.

PNG image:

enter image description here

PNG Resized image 1:

enter image description here

PNG Resized image 2:

enter image description here

Asif Raza
  • 836
  • 12
  • 29
  • it should be down you compress and resize to non retina display – Abdelahad Darwish Aug 21 '17 at 08:51
  • when you want to display image and its size is doubled {750, 1334} you can display by this UIImage * img = {750, 1334}.; img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:img.imageOrientation]; – Abdelahad Darwish Aug 21 '17 at 08:54
  • My concern is not to display it. My requirement is to save in Camera roll with half of the original image size with same quality. – Asif Raza Aug 21 '17 at 09:01
  • Actually I am trying to add image over video as thumbnail. My Video size is {320, 568} and image size is {750, 1334}. When I am adding it using AVFoundation then CALayer of image resizes image to the size of video. Then my image quality goes down. That's why I want to resize it firs with same quality. Then want to add at video. If you have any idea, please help. – Asif Raza Aug 21 '17 at 09:05
  • check this https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos – Abdelahad Darwish Aug 21 '17 at 09:07
  • Thanks, I already following it. – Asif Raza Aug 21 '17 at 09:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152421/discussion-between-asif-raza-and-abdelahad-darwish). – Asif Raza Aug 21 '17 at 09:13
  • Guess what you want is something to [ImageOptim](https://github.com/ImageOptim/ImageOptim), which use various image optimization to recompress without losing quality, dont think there's already made library for that, it's open source though, you can try apply it to your project – Tj3n Aug 21 '17 at 09:14
  • Hay @AsifRaza how to resolve this issue. – Chirag Kothiya Jul 12 '18 at 14:01

1 Answers1

0

Resize with Good Quality

 - (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    {
        // Create a bitmap context.
        UIGraphicsBeginImageContextWithOptions(newSize, YES, image.scale);
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;

    }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35