最近遇到了 APP 启动页面(第一次启动时)图片略大的问题,因此想到了等比率缩放的问题。
一番查找之后,果真找到了解决办法,缩放方法的代码如下:
- (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;
}