resizing UIImage the fastest and efficient way

前端 未结 2 1044
既然无缘
既然无缘 2021-01-05 15:17

I wanted to resize a UIImage to a certain width and height keeping the proportion in place. The simplest way to do this is:

 CGSize newSize = CGSizeMake(726,         


        
相关标签:
2条回答
  • 2021-01-05 15:56

    Here is a simple way:

        UIImage * image = [UIImage imageNamed:@"image"];
        CGSize sacleSize = CGSizeMake(10, 10); // scale image to 10 x 10
        UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
        UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

    resizedImage is a new image.

    0 讨论(0)
  • 2021-01-05 16:18

    In a performance sample of Image Resizing Techniques the score was

    • 0.1420 UIKit
    • 0.1722 Core Graphics
    • 0.1616 Image I/O
    • 2.4983 Core Image
    • 2.3126 vImage

    so the fastest and simplest way is:

    
    let renderer = UIGraphicsImageRenderer(size: size)
    let resized = renderer.image { (context) in
        image.draw(in: CGRect(origin: .zero, size: size))
    }
    

    Resize a UIImage the right way is from 2009 but contains useful talk about interpolation quality and preserving the aspect ratio.

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