如何将大图像变成小图像,并且质量与原始图像一样好?

发布于 2024-11-25 11:11:44 字数 130 浏览 0 评论 0原文

我使用尺寸为 80X80 的 imagview 来显示大图像(1024 X 780)。

将大图像放入图像视图时,图像看起来被挤压、压缩,不像质量图像。

我的问题是,如何将大图像变成小图像,并且质量与原始图像一样好?

I am using imagview with size of 80X80 to display large image (1024 X 780).

While placing the large image into the imageview, the image looks squeezed, compressed something not like the quality one.

My question is, how can I make the large image into small with as good quality as the original image ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

临风闻羌笛 2024-12-02 11:11:44

在图像视图中,请将imageview.mode设为中心,而不是缩放以适合或任何其他。
我相信这将使您的任何缩放图像在 80x80 尺寸下都具有最佳质量

In the image view, please make the imageview.mode as centre not scale to fit or any other.
I believe this would put ur any scaled image at finest quality in ur 80x80 dimension.

飘过的浮云 2024-12-02 11:11:44

在这种情况下,您应该对要在 ImageView 中显示的图像进行适当的缩放

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize 
{
    UIImage *sourceImage = chosenImage;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor < heightFactor) 
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor*1;
        scaledHeight = height * scaleFactor;

        // center the image

        if (widthFactor < heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }


    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}

In this case, you should do proper Scaling of the Image that u want to show in the ImageView

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize 
{
    UIImage *sourceImage = chosenImage;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor < heightFactor) 
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor*1;
        scaledHeight = height * scaleFactor;

        // center the image

        if (widthFactor < heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }


    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}
家住魔仙堡 2024-12-02 11:11:44

[imageView setContentMode:UIViewContentModeScaleAspectFill];

但是,如果您想多次使用图像,则应该考虑调整图像大小并将其保存在磁盘上。
要调整图像大小:

调整 UIImage 大小的最简单方法?

< a href="https://stackoverflow.com/questions/2645768/uiimage-resize-scale-proportion">UIImage 调整大小(缩放比例)

UIImage:调整大小,然后裁剪

等。

[imageView setContentMode:UIViewContentModeScaleAspectFill];

You should however consider resizing the image and saving it on the disk if you want to use it more then once.
To resize the image:

The simplest way to resize an UIImage?

UIImage resize (Scale proportion)

UIImage: Resize, then Crop

etc..

戏剧牡丹亭 2024-12-02 11:11:44

从 1034x780 变为 80x80 实际上不可能保持质量,因为没有足够的空间来捕捉所有细节。

您可以尝试缩放它:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Going from 1034x780 into 80x80 is virtually impossible to keep the quality, as there isn't enough space to capture all the details.

You can try to scale it:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
嘴硬脾气大 2024-12-02 11:11:44

您可以设置图像模式 [imageView setContentMode:UIViewContentModeScaleAspectFill];

imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
[imageView setClipsToBounds:YES];

you can set image mode [imageView setContentMode:UIViewContentModeScaleAspectFill];

imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
[imageView setClipsToBounds:YES];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文