UIImage categories (part 3 of 4)
Once I have the smartly cropped image, I then need to scale it to fit inside a particular rectangle. Here is the code to do that, courtesy of the NYXImagesKit by Nyx0uf:
- (UIImage*)scaleToFitSize:(CGSize)newSize { /// Keep aspect ratio size_t destWidth, destHeight; if (self.size.width > self.size.height) { destWidth = (size_t)newSize.width; destHeight = (size_t)(self.size.height * newSize.width / self.size.width); } else { destHeight = (size_t)newSize.height; destWidth = (size_t)(self.size.width * newSize.height / self.size.height); } if (destWidth > newSize.width) { destWidth = (size_t)newSize.width; destHeight = (size_t)(self.size.height * newSize.width / self.size.width); } if (destHeight > newSize.height) { destHeight = (size_t)newSize.height; destWidth = (size_t)(self.size.width * newSize.height / self.size.height); } return [self scaleToFillSize:CGSizeMake(destWidth, destHeight)]; } |
BTW, I hope everyone is having a good Memorial Day weekend. Please pause and remember well those that made the ultimate sacrifice for us.