I want to grab a subimage from a UIImage. I've looked around for a similar question, to no avail.
I know the range of pixels I want to grab - how can I return this subimage, from an existing image?
This should help: http://iphonedevelopment.blogspot.com/2010/11/drawing-part-of-uiimage.html
This code snippet is creating a category of UIImage but the code should be easily modified to work without it being a category.
A shorter way of doing the same thing is the following:
CGRect fromRect = CGRectMake(0, 0, 320, 480); // or whatever rectangle CGImageRef drawImage = CGImageCreateWithImageInRect(image.CGImage, fromRect); UIImage *newImage = [UIImage imageWithCGImage:drawImage]; CGImageRelease(drawImage); Hope this helps!
image.scale), apply it to the rect (multiply all numeric values by that factor) and when creating the final UIImage, use the method imageWithCGImage:scale:orientation: to set the correct scale factor again.In Swift 4, taking into account screen scale (otherwise your new image will be too large):
let img = UIImage(named: "existingImage")! let scale = UIScreen.main.scale let dy: CGFloat = 6 * scale // say you want 6pt from bottom let area = CGRect(x: 0, y: img.size.height * scale - dy, width: img.size.width * scale, height: dy) let crop = img.cgImage!.cropping(to: area)! let subImage = UIImage(cgImage: crop, scale: scale, orientation:.up)