How to draw full UIImage inside a square with whit

2019-09-18 04:44发布

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.

PS wich methods of CGcontext do i Need to call.

thanks.

- (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.

enter image description here

2条回答
可以哭但决不认输i
2楼-- · 2019-09-18 05:17

In Swift 3

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
    }

}
查看更多
Ridiculous、
3楼-- · 2019-09-18 05:25

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

Which takes this sample image Before Image, Rectangular

and gives you this new square image Processed Image, Square

查看更多
登录 后发表回答