Resize Image without losing quality

后端 未结 4 992
天命终不由人
天命终不由人 2021-01-02 04:50

I\'m using the functions below to resize my images width & height but I noticed that it ruins the image quality.

class func imageWithSize(image: UIImage,         


        
4条回答
  •  不知归路
    2021-01-02 05:14

    Here is my Objective-C code. It's work for me.

    + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize isAspectRation:(BOOL)aspect {
        if (!image) {
            return nil;
        }
        //UIGraphicsBeginImageContext(newSize);
        // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
        // Pass 1.0 to force exact pixel size.
        CGFloat originRatio = image.size.width / image.size.height;
        CGFloat newRatio = newSize.width / newSize.height;
    
        CGSize sz;
    
        if (!aspect) {
            sz = newSize;
        }else {
            if (originRatio < newRatio) {
                sz.height = newSize.height;
                sz.width = newSize.height * originRatio;
            }else {
                sz.width = newSize.width;
                sz.height = newSize.width / originRatio;
            }
        }
        CGFloat scale = 1.0;
        //    if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
        //        CGFloat tmp = [[UIScreen mainScreen]scale];
        //        if (tmp > 1.5) {
        //            scale = 2.0;
        //        }
        //    }
        sz.width /= scale;
        sz.height /= scale;
        UIGraphicsBeginImageContextWithOptions(sz, NO, scale);
        [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    I saw your code in Swift. I just convert my Objective-C to Swift without test. Can you try it and let's me know. Thanks!

    struct CommonUtils {
        static func imageWithImage(image: UIImage, scaleToSize newSize: CGSize, isAspectRation aspect: Bool) -> UIImage{
    
            let originRatio = image.size.width / image.size.height;//CGFloat
            let newRatio = newSize.width / newSize.height;
    
            var sz: CGSize = CGSizeZero
    
            if (!aspect) {
                sz = newSize
            }else {
                if (originRatio < newRatio) {
                    sz.height = newSize.height
                    sz.width = newSize.height * originRatio
                }else {
                    sz.width = newSize.width
                    sz.height = newSize.width / originRatio
                }
            }
            let scale: CGFloat = 1.0
    
            sz.width /= scale
            sz.height /= scale
            UIGraphicsBeginImageContextWithOptions(sz, false, scale)
            image.drawInRect(CGRectMake(0, 0, sz.width, sz.height))
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }
    }
    

提交回复
热议问题