Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何裁剪偏移量为10像素的图像?_Ios_Swift_Uiimage - Fatal编程技术网

Ios 如何裁剪偏移量为10像素的图像?

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

这是我的输入图像:

这就是我需要的输出(无10px填充)

有没有办法在Swift中实现这一点?

您可以使用Core Graphics方法裁剪
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