简体   繁体   English

IOS 如何根据实际图像比例调整图像大小并给出自定义大小?

[英]IOS How to resize image according to actual image ratio and give custom size?

Resize UIImage with custom size by checking actual image ratio for reducing image size.通过检查实际图像比例来调整UIImagecustom size以减小图像大小。 I have already tried this UIImageJPEGRepresentation(Image, 0.8);我已经试过这个UIImageJPEGRepresentation(Image, 0.8); but problem with this is that image quality is reducing.但问题是图像质量正在降低。 Any help would be really appreciated.任何帮助将非常感激。 Thank you谢谢

Pass UIImage to below method which resize your image with custom height/width and you can also give compression value of image for reducing image size and Check image size before and after resizing.UIImage传递给以下方法,该方法使用自定义高度/宽度调整图像大小,您还可以提供图像的压缩值以减小图像大小并检查调整大小前后的图像大小。

Here is sample method-这是示例方法-

-(UIImage *)resizeImage:(UIImage *)image
{

    NSInteger imageActualSize = UIImageJPEGRepresentation(image,1).length;

    NSLog(@"size of IMAGE before resizing: %@ ", [NSByteCountFormatter stringFromByteCount:imageActualSize countStyle:NSByteCountFormatterCountStyleFile]);

    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = 400.0; // your custom  height
    float maxWidth = 350; // your custom  width
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;
    float compressionQuality = 0.5;//50 percent compression

    if (actualHeight > maxHeight || actualWidth > maxWidth)
    {
        if(imgRatio < maxRatio)
        {
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio)
        {
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else
        {
            actualHeight = maxHeight;
            actualWidth = maxWidth;
        }
    }

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    UIGraphicsEndImageContext();

    NSInteger imageReduceSize = imageData.length;

    NSLog(@"size of IMAGE after resizing: %@ ",[NSByteCountFormatter stringFromByteCount:imageReduceSize countStyle:NSByteCountFormatterCountStyleFile]);

    return [UIImage imageWithData:imageData];

}

This will help you..:)这会帮助你..:)

+ (UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

very easy way to do this.非常简单的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据图像大小调整UIImageView的大小? - Resize UIImageView according to image size? UIImage imageWithData如何查找图像大小和调整大小比例 - UIImage imageWithData how to find image size and resize ratio 如何使用宽高比调整图像大小? - How to resize image with aspect ratio? 在保留宽高比的同时将图像调整为一定大小,并在必要时裁剪图像(iOS) - Resize Image to certain size while retaining aspect ratio, crop image if necessary (iOS) 在iOS中调整图像大小并保持其纵横比 - Resize image and maintain its aspect ratio in iOS 如何使用 iOS 轻松调整/优化图像大小? - How to easily resize/optimize an image size with iOS? 如何根据文本调整导航后退按钮的大小(不使用自定义图像) - How to resize navigation back button according to text (not using custom image) 如何使用 SwiftUI 以 1:1 的比例自动调整大小的图像? - How to setup Image with SwiftUI that resize automatically with ratio 1:1? 如何在iOS上的UIImagePicker中获取实际图像帧的大小? - How to get the size of the actual image frame inside UIImagePicker on iOS? 如何在不使用自定义单元格的情况下在tableviewcell上显示实际图像大小? - How to show actual image size on tableviewcell without using custom cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM