4

I'm currently Adding a rectangle to an image in case it's not squared "filling" the remaining space, making it square. The idea is that this is a cheap approach (in terms of time required to "force" an image to be squared).

As you can see in the image attached, the square I generate does not have a color, and this might confuse the user. This is why I'd like to add a color to the CGRect I'm generating. The problem is I've not found any way to do this. Any suggestions on how to give color to this square generated, would be greately appreciated.

enter image description here

enter image description here

The current code I have to do this is the following:

func forceSquaredImage(image: UIImage) -> UIImage{
    let maxDimension = max(image.size.height, image.size.width)

    var newImage : UIImage = image

    if(image.size.height > image.size.width){

        UIGraphicsBeginImageContextWithOptions(CGSize(width: maxDimension, height: maxDimension), false, 0.0);

        image.drawInRect(CGRectMake(image.size.height/2-image.size.width/2, 0, image.size.width, image.size.height))
        newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

    }else if(image.size.height < image.size.width){

        UIGraphicsBeginImageContextWithOptions(CGSize(width: maxDimension, height: maxDimension), false, 0.0);
        image.drawInRect(CGRectMake(0, image.size.width/2-image.size.height/2, image.size.width, image.size.height))
        newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage

    }

    return newImage

}

The desired result would be something like this (color red is just an example in this case):

enter image description here

Waclock
  • 1,686
  • 19
  • 37

3 Answers3

4

Just fill with a background color before you draw the image:

func forceSquaredImage(image: UIImage) -> UIImage{
    let maxDimension = max(image.size.height, image.size.width)

    var newImage : UIImage = image
    UIGraphicsBeginImageContextWithOptions(CGSize(width: maxDimension, height: maxDimension), false, 0.0);
    UIColor.redColor().set()
    UIBezierPath(rect: CGRect(x:0, y: 0, width:maxDimension, height:maxDimension)).fill()
    if(image.size.height > image.size.width) {
        image.drawInRect(CGRectMake(image.size.height/2-image.size.width/2, 0, image.size.width, image.size.height))
        }
    else if (image.size.height < image.size.width) {
        image.drawInRect(CGRectMake(0, image.size.width/2-image.size.height/2, image.size.width, image.size.height))
        }
        newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
}
Waclock
  • 1,686
  • 19
  • 37
Aderstedt
  • 6,301
  • 5
  • 28
  • 44
  • That's what I said as well. I try to avoid providing code, as the OP is more likely to learn if he writes the code himself. – Duncan C Sep 25 '15 at 19:41
  • Note that this is an iOS question, so you should use UIColor, not NSColor. (I edited your answer) – Duncan C Sep 25 '15 at 19:41
  • I tried using your code, had to make a few changes to the NSBezierPath, to : UIBezierPath(rect: CGRect(x:0, y: 0, width:maxDimension, height:maxDimension)) However I get the following warning: Result of initializer is unused and I get the same result as before :( – Waclock Sep 25 '15 at 19:56
2

Try this: (not tested, may need adjustment)

Create a UIBezierPath that's the full size of your off-screen context using bezierPathWithRect.

Use the UIColor method set (or setFill) to set the current stroke/fill or fill color to the receiver.

Use the UIBezierPath method fill to fill the path with the current fill color in your graphics context.

Then draw your image into the context as you are doing now.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

I'd like to add a color to the CGRect I'm generating.

How is:

let colorView = UIView(frame: myGeneratedRect)
colorView.backgroundColor = .red

?

AmitaiB
  • 1,656
  • 1
  • 19
  • 19