Ios 如何裁剪偏移量为10像素的图像?
这是我的输入图像: 这就是我需要的输出(无10px填充): 有没有办法在Swift中实现这一点?您可以使用Core Graphics方法裁剪Ios 如何裁剪偏移量为10像素的图像?,ios,swift,uiimage,Ios,Swift,Uiimage,这是我的输入图像: 这就是我需要的输出(无10px填充): 有没有办法在Swift中实现这一点?您可以使用Core Graphics方法裁剪CGImage。只需实现一个自定义裁剪方法,以采用top、bottom、left和right,而不是CGRect参数,如下所示: extension CGImage { var image: UIImage { return .init(cgImage: self) } func cropping(top: CGFloat, bottom
CGImage
。只需实现一个自定义裁剪方法,以采用top、bottom、left和right,而不是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