iOS调整大小并保存为已调整大小的图像

时间:2015-10-09 08:36:33

标签: ios objective-c uiimageview uiimage

代码工作正常,但是当我保存此图像时,它会保存为原始大小。我如何将其保存为缩放尺寸?

- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {

CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if( image.size.width > image.size.height ) {
    scaleFactor = image.size.width / image.size.height;
    scaledSize.width = newSize.width;
    scaledSize.height = newSize.height / scaleFactor;
}
else {
    scaleFactor = image.size.height / image.size.width;
    scaledSize.height = newSize.height;
    scaledSize.width = newSize.width / scaleFactor;
}

UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

   return scaledImage;
}

2 个答案:

答案 0 :(得分:2)

将UIGraphicsBeginImageContextWithOptions中的比例因子设置为1.0。将其设置为0.0会将比例设置为设备的屏幕比例因子。

答案 1 :(得分:0)

在界面构建器中,有一个名为" mode"的UIImageView属性。尝试从那里设置它。