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.
In Swift 3
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.
Which takes this sample image Before Image, Rectangular
and gives you this new square image Processed Image, Square