2016-11-28 2 views
0

특정 너비와 높이의 가운데에서 이미지를 자르고 싶습니다. 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 
} 

어떻게하면됩니까?

+1

참조 : http://stackoverflow.com/a/712553/1457385 – shallowThought

답변

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

나는이 비율을 사용하여 내 프레임과 다른 비율의 이미지를 표시하므로 중심에 중첩하여 자릅니다.

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 
    } 
} 
0

이 있어야한다 :

func cropImage(toRect rect:CGRect) -> UIImage? { 
    var rect = rect 
    rect.origin.y = rect.origin.y * self.scale 
    rect.origin.x = rect.origin.x * self.scale 
    rect.size.width = rect.width * self.scale 
    rect.size.height = rect.height * self.scale 

    guard let imageRef = self.cgImage?.cropping(to: rect) else { 
     return nil 
    } 

    let croppedImage = UIImage(cgImage:imageRef) 
    return croppedImage 
} 
+0

무기 호는 반환 형식과 호환되지 않는 '있는 UIImage' – Alfi