如何在边缘为白色的正方形内绘制完整的 UIImage

How to draw full UIImage inside a square with white color on the edge

您好,就像我在标题中所说的那样,我正在尝试在与 UIImage 的最大尺寸相匹配的正方形内绘制完整的 UIImage。我的问题是我怎样才能让我的代码工作。我的代码所做的是裁剪成正方形大小。我是 IOs 的新手,谁能帮忙。

PS 我需要调用 CGcontext 的哪些方法。

谢谢。

- (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;
}

我正在寻找的结果类型。

看起来您比您意识到的更接近实现您的目标。您需要调整调用 drawInRect 时使用的矩形:以保持纵横比并适合新图像矩形的中间。请记住,此处使用的矩形不决定新图像的大小,传递给 UIGraphicsBeginImageContext 的值定义了它。

- (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;
}

使用这张示例图片 Before Image, Rectangular

并给你这张新的方形图片 Processed Image, Square

在Swift3

extension UIImage {

    func squareMe() -> UIImage {

        var squareImage = self
        let maxSize = max(self.size.height, self.size.width)
        let squareSize = CGSize(width: maxSize, height: maxSize)

        let dx = CGFloat((maxSize - self.size.width) / 2)
        let dy = CGFloat((maxSize - self.size.height) / 2)

        UIGraphicsBeginImageContext(squareSize)
        var rect = CGRect(x: 0, y: 0, width: maxSize, height: maxSize)

        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(UIColor.clear.cgColor)
            context.fill(rect)

            rect = rect.insetBy(dx: dx, dy: dy)
            self.draw(in: rect, blendMode: .normal, alpha: 1.0)

            if let img = UIGraphicsGetImageFromCurrentImageContext() {
                squareImage = img
            }
            UIGraphicsEndImageContext()

        }

        return squareImage
    }

}