如何用颜色填充图像视图中的空白(未被图像覆盖)?

发布于 2024-12-05 06:10:02 字数 1282 浏览 1 评论 0原文

我想创建一个固定大小的图像,例如 612 x 612。我正在使用图像选择器从我的 iPhone 中选择照片。因此,为了确保所有照片都适合 612 x 612 尺寸且不变形,我使用以下方法重新缩放照片,使其符合 612 x 612 尺寸。然而,结果出现了空格可能会在最终图像中创建。 (参见下面的示例)

我使用以下代码来缩放图像(固定大小 612 x 612)

//Scale the image to fit to imageview
UIImage *image = [self scaleImage:img toRectSize:CGRectMake(0, 0, 612, 612)];


//Method to scale image
- (UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect
{
    UIGraphicsBeginImageContext(screenRect.size);
    float hfactor = img.size.width / screenRect.size.width;
    float vfactor = img.size.height / screenRect.size.height;

    float factor = MAX(hfactor, vfactor);

    float newWidth = img.size.width / factor;
    float newHeight = img.size.height / factor;

    float leftOffset = (screenRect.size.width - newWidth) / 2;
    float topOffset = (screenRect.size.height - newHeight) / 2;

    CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
    [img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;   
}

如上所述,由于图像有时不是完整的正方形,我得到的结果如下所示:

在此处输入图像描述

如何用黑色感受图像中的空白?

I want to create an image of fixed size e.g. 612 by 612. I am using an image picker to select photos from my iphone. So in order to ensure that all the photos fit to the 612 by 612 size without distortion, I am using the following method to re-scale the photo so that they conform to the size of 612 by 612. However as a result, blank spaces might be created in the final image. (see example below)

I am using the following code to scale my image (of fixed size 612 by 612)

//Scale the image to fit to imageview
UIImage *image = [self scaleImage:img toRectSize:CGRectMake(0, 0, 612, 612)];


//Method to scale image
- (UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect
{
    UIGraphicsBeginImageContext(screenRect.size);
    float hfactor = img.size.width / screenRect.size.width;
    float vfactor = img.size.height / screenRect.size.height;

    float factor = MAX(hfactor, vfactor);

    float newWidth = img.size.width / factor;
    float newHeight = img.size.height / factor;

    float leftOffset = (screenRect.size.width - newWidth) / 2;
    float topOffset = (screenRect.size.height - newHeight) / 2;

    CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);
    [img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;   
}

As mentioned, as the image is sometimes not a full square, I get a result like what you see below:

enter image description here

How can I feel up the white spaces in the image with black color?

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

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

发布评论

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

评论(2

苍白女子 2024-12-12 06:10:02

一种方法是将 UIImageView 的 backgroundColor 设置为 blackColor

另一种方法是在缩放图像时用 blackColor 填充矩形。

- (UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect {

    ...
    CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);

    // Fill the original rect with black color
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextFillRect(context, screenRect);

    [img drawInRect:newRect blendMode:kCGBlendModeNormal alpha:1];
    ...
}

请注意,drawInRect:blendMode:alpha: 方法中的 blendMode 设置为 kCGBlendModeNormal。如果您设置一些其他混合模式,您将得到不想要的结果。例如,如果将混合模式设置为kCGBlendModePlusDarker并用blackColor填充矩形,则整个图像将变成黑色。

One way is to set the backgroundColor of UIImageView to blackColor.

Another way is to fill the rect with blackColor while you scale the image.

- (UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect {

    ...
    CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight);

    // Fill the original rect with black color
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextFillRect(context, screenRect);

    [img drawInRect:newRect blendMode:kCGBlendModeNormal alpha:1];
    ...
}

Note that the blendMode in drawInRect:blendMode:alpha: method is set to kCGBlendModeNormal. If you set some other blend modes you will get undesired results. For example, if you set the blend mode to kCGBlendModePlusDarker and fill the rect with blackColor then the entire image will become black.

心意如水 2024-12-12 06:10:02

UIImageView 上的背景颜色设置为黑色,你就会得到你想要的

Set the background color on your UIImageView to black and you'll get what you want

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