2016-11-28 12 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

関連する問題