如何在边缘为白色的正方形内绘制完整的 UIImage [英] How to draw full UIImage inside a square with white color on the edge

查看:22
本文介绍了如何在边缘为白色的正方形内绘制完整的 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我在标题中所说的那样,我正在尝试在与 UIImage 的最大尺寸匹配的正方形内绘制完整的 UIImage.我的问题是如何让我的代码工作.我的代码正在裁剪到正方形大小.我是 IO 新手,任何人都可以提供帮助.

Hi like i said on the title i'm trying to draw a full UIImage inside a square that match the highest size of the UIImage. my question is how can i make my code work. what my code is dowing is cropping to square size. I'm new to IOs can anyOne help.

我需要调用CGcontext的方法.

PS wich methods of CGcontext do i Need to call.

谢谢.

- (UIImage*)imageWithBorderFromImage:(UIImage*)source;
{
    CGSize size = [source size];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGSize resizedImageSize;
    if (width < height) {
        resizedImageSize = CGSizeMake(height, height);
    }else {
        resizedImageSize = CGSizeMake(width, width);
    }
    UIGraphicsBeginImageContext(resizedImageSize);
    CGRect rect = CGRectMake(0, 0, resizedImageSize.width, resizedImageSize.height);
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0);
    CGContextStrokeRect(context, rect);
    UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return testImg;
}

我正在寻找的结果类型.

The type of result i'm looking for.

推荐答案

看起来您离实现目标更近了.您需要调整调用 drawInRect: 时使用的矩形,以保持纵横比并适合新图像矩形的中间.请记住,那里使用的 rect 并没有规定新图像的大小,传递给 UIGraphicsBeginImageContext 的值定义了这一点.

It looks like you were closer than you realized to achieving your goal. You needed to adjust the rect used when calling drawInRect: to keep the aspect ratio and fit in the middle of the new image's rect. Remember that the rect used there does not dictate the new image's size, the value passed to UIGraphicsBeginImageContext defines that.

- (UIImage *)squareImageFromImage:(UIImage *)image
{
    // Get a square that the image will fit into
    CGFloat maxSize = MAX(image.size.height, image.size.width);
    CGSize squareSize = CGSizeMake(maxSize, maxSize);

    // Get our offset to center the image inside our new square frame
    CGFloat dx = (maxSize - image.size.width) / 2.0f;
    CGFloat dy = (maxSize - image.size.height) / 2.0f;

    UIGraphicsBeginImageContext(squareSize);
    CGRect rect = CGRectMake(0, 0, maxSize, maxSize);

    // Draw a white background and set a magenta stroke around the entire square image (debugging only?)
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor magentaColor].CGColor);
    CGContextFillRect(context, rect);
    CGContextStrokeRect(context, rect);

    // Adjust the rect to be centered in our new image
    rect = CGRectInset(rect, dx, dy);
    [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

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

    return squareImage;
}

使用此示例图像前图像,矩形

并为您提供这个新的方形图像处理后的图像,方形

and gives you this new square image Processed Image, Square

这篇关于如何在边缘为白色的正方形内绘制完整的 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆