IOS image processing-cropping Images
Solution:Based on the ratio of a certain length to width, the upper and lower parts of the image are cropped to retain the content in the middle. You can also customize the area to be cropped.
Prerequisites:You need to add the Framework: CoreGraphics. framework.
Code:
- (UIImage*) crop:(UIImage*)theImage{ // Get size of current image CGSize size = [theImage size]; // Create rectangle that represents a cropped image CGFloat desiredRatio = 1.2; CGFloat croppedWidth = 0.0; CGFloat croppedHeight = 0.0; CGRect rect; if (size.height/size.width >= desiredRatio) { croppedWidth = size.width; croppedHeight = size.width * desiredRatio; CGFloat difference = (size.height-croppedHeight)/2; rect = CGRectMake(0.0, difference ,croppedWidth, croppedHeight); } else{ return theImage; } // Create bitmap image from original image data, // using rectangle to specify desired crop area CGImageRef imageRef = CGImageCreateWithImageInRect([theImage CGImage], rect); UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return croppedImage;}
Reference: online resources, links lost