Newer
Older
XinYang_IOS / XYSW / YHScanQRTool / UIImage+YHImageSize.m
@zhangfeng zhangfeng on 7 Dec 2023 1 KB 1.8.0
//
//  UIImage+YH_ImageSize.m
//  YHScanQR
//
//  Created by Foxconn on 2018/1/20.
//  Copyright © 2018年 Foxconn. All rights reserved.
//

#import "UIImage+YHImageSize.h"
#define YHImageScreenWidth [UIScreen mainScreen].bounds.size.width
#define YHImageScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation UIImage (YHImageSize)

/// 返回一张不超过屏幕尺寸的 image
+ (UIImage *)imageSizeWithScreenImage:(UIImage *)image {
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat screenWidth = YHImageScreenWidth;
    CGFloat screenHeight = YHImageScreenHeight;
    
    if (imageWidth <= screenWidth && imageHeight <= screenHeight) {
        return image;
    }
    
    CGFloat max = MAX(imageWidth, imageHeight);
    CGFloat scale = max / (screenHeight * 2.0);
    
    CGSize size = CGSizeMake(imageWidth / scale, imageHeight / scale);
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end