Swift 3:裁剪图像

时间:2016-11-28 10:49:06

标签: ios swift uiimage

我想从中心裁剪具有特定宽度和高度的图像。我在SO问题中发现了这个代码,但是这个方法会调整图像大小然后裁剪它。我想只拍摄我的图像而不调整大小。我尝试修改此代码,但无法得到我想要的结果。

//cropImage
func cropToBounds(image: UIImage, width: Double, height: Double) -> UIImage {
let contextImage: UIImage = UIImage(cgImage: image.cgImage!) 

let contextSize: CGSize = contextImage.size
var posX: CGFloat = 0.0
var posY: CGFloat = 0.0
var cgwidth: CGFloat = CGFloat(width)
var cgheight: CGFloat = CGFloat(height)
// See what size is longer and create the center off of that

if contextSize.width > contextSize.height {
    posX = ((contextSize.width - contextSize.height) / 2)
    posY = 0
    cgwidth = contextSize.height
    cgheight = contextSize.height
} else {
    posX = 0
    posY = ((contextSize.height - contextSize.width) / 2)
    cgwidth = contextSize.width
    cgheight = contextSize.width
}
let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)
// Create bitmap image from context using the rect
let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!

// Create a new image based on the imageRef and rotate back to the original orientation
let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)

return image
} 

我该怎么做?

4 个答案:

答案 0 :(得分:1)

这应该可以正常工作:

func cropToBounds(image: UIImage, width: Double, height: Double) -> UIImage {
let contextImage: UIImage = UIImage(cgImage: image.cgImage!) 

let contextSize: CGSize = contextImage.size
var posX: CGFloat = contextSize.width
var posY: CGFloat = contextSize.width
var cgwidth: CGFloat = CGFloat(width)
var cgheight: CGFloat = CGFloat(height)
// See what size is longer and create the center off of that


let rect: CGRect = CGRect(x: posX-cgwidth/2, y: posY-cgheight/2, width: cgwidth, height: cgheight)
// Create bitmap image from context using the rect
let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!

// Create a new image based on the imageRef and rotate back to the original orientation
let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)

return image
} 

希望这有帮助。

答案 1 :(得分:0)

应该是:

{{1}}

答案 2 :(得分:0)

  1. 确保图像将在中央裁剪。
  2. 包括宽度和高度给定的最大作物区域。
    extension UIImage {

        func cropImage(width: CGFloat = 67, height: CGFloat = 67) -> UIImage {

            let scale = min(self.size.width / width,
                            self.size.height / height)

            // x, y will crop image in the center area
            let x = self.size.width > self.size.height 
                ? (self.size.width - width * scale) / 2 
                : 0

            let y = self.size.width > self.size.height 
                ? 0 
                : (self.size.height - height * scale) / 2

            let cropZone = CGRect(
                x: x, y: y,
                width: width * scale,
                height: height * scale)

            guard let image: CGImage = self.cgImage?.cropping(to: cropZone)
            else { return UIImage() }

            return UIImage(cgImage: image)
        }
    }

答案 3 :(得分:-1)

我用它来显示比例与我的画面不同的图像,所以我将它居中并裁剪边。

extension UIImage {
func cropTo(size: CGSize) -> UIImage {
    guard let cgimage = self.cgImage else { return self }

    let contextImage: UIImage = UIImage(cgImage: cgimage)

    var cropWidth: CGFloat = size.width
    var cropHeight: CGFloat = size.height

    if (self.size.height < size.height || self.size.width < size.width){
        return self
    }

    let heightPercentage = self.size.height/size.height
    let widthPercentage = self.size.width/size.width

    if (heightPercentage < widthPercentage) {
        cropHeight = size.height*heightPercentage
        cropWidth = size.width*heightPercentage
    } else {
        cropHeight = size.height*widthPercentage
        cropWidth = size.width*widthPercentage
    }

    let posX: CGFloat = (self.size.width - cropWidth)/2
    let posY: CGFloat = (self.size.height - cropHeight)/2

    let rect: CGRect = CGRect(x: posX, y: posY, width: cropWidth, height: cropHeight)

    // Create bitmap image from context using the rect
    let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!

    // Create a new image based on the imageRef and rotate back to the original orientation
    let cropped: UIImage = UIImage(cgImage: imageRef, scale: self.scale, orientation: self.imageOrientation)

    return cropped
   }
}