특정 너비와 높이의 가운데에서 이미지를 자르고 싶습니다. SO 문제에서이 코드를 발견했지만이 방법은 이미지 크기를 조정 한 다음 잘라냅니다. 내 이미지를 자르고 크기를 조정하지 않고 싶습니다. 이 코드를 수정하려고했지만 원하는 결과를 얻을 수 없습니다.스위프트 3 : 자르기 이미지
//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
}
어떻게하면됩니까?
참조 : http://stackoverflow.com/a/712553/1457385 – shallowThought