drawInRect:
Draws the entire image in the specified rectangle, scaling it as needed to fit.
- (void)drawInRect:(CGRect)rect
Parameters
- rect
- The rectangle (in the coordinate system of the graphics context) in which to draw the image.
Discussion of [UIImage drawInRect]
This method draws the entire image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the origin of the specified rectangle. This method respects any transforms applied to the current graphics context, however.[UIImage drawInRect]
This method draws the image at full opacity using the
kCGBlendModeNormal
blend mode.
Example of [UIImage drawInRect]
- (UIImage *)imageScaledToSize:(CGSize)size
{
//create drawing context
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//draw
[self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return image
return image;
}
- (UIImage *)imageScaledToFitSize:(CGSize)size
{
//calculate rect
CGFloat aspect = self.size.width / self.size.height;
if (size.width / aspect <= size.height)
{
return [self imageScaledToSize:CGSizeMake(size.width, size.width / aspect)];
}
else
{
return [self imageScaledToSize:CGSizeMake(size.height * aspect, size.height)];
}
}