Вы можете использовать Core Graphics метод обрезки , чтобы обрезать CGImage
.Просто внедрите собственный метод обрезки, чтобы взять верх, низ, левый и правый вместо параметра CGRect
следующим образом:
extension CGImage {
var image: UIImage { return .init(cgImage: self) }
func cropping(top: CGFloat, bottom: CGFloat, left: CGFloat, right: CGFloat) -> CGImage? {
let width = CGFloat(self.width)
let height = CGFloat(self.height)
precondition(left + right < width && top + bottom < height)
return cropping(to: .init(origin: .init(x: left, y: top), size: .init(width: width-left-right, height: height-top-bottom) ))
}
}
Тестирование игровой площадки
let image = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))!
let cropped = image.cgImage?.cropping(top: 100, bottom: 100, left: 100, right: 100)?.image