这两天处理引导页面的时候遇到了图片略大的问题,上网查找后找到了解决方法。用的是 UIImage 的等比率缩放,虽然不难,但之前没接触过,故记之。
代码如下:
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
我用的时候稍作了改动(为了让图片更好的适应各个尺寸的屏幕,这里的缩放是根据屏幕的大小来的),代码如下:
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{ UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width * scaleSize, [UIScreen mainScreen].bounds.size.width * scaleSize)); [image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * scaleSize, [UIScreen mainScreen].bounds.size.width * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
主要参考:关于UIImage图片处理详细使用