swift 画像サイズ変更トリミング

PikerViewを利用してカメラまたはギャラリーよりの写真を送信する際に写真のorientationを考慮し
更に正方形にトリミングするプログラム記述してみました。取りあえず動いてます。

１、トリミングとリサイズ関数

```swift:swift    
    func cropImageToSquare(image: UIImage) -> UIImage? {
        
        // 画像サイズをスクリーン幅に合わせる
        
        let ref: CGImageRef = image.CGImage!
        var srcWidth: Int = CGImageGetWidth(ref)
        var srcHeight: Int = CGImageGetHeight(ref)
        print(srcWidth)
        print(srcHeight)
        let width = image.size.width
        let height = image.size.height
        
        
        if(width > height){
            let scrsize = self.view.bounds.height
            let scale = scrsize / width
            let widthx: Int = Int(width * scale)
            let heightx: Int = Int(height * scale)
            
            let image = resizeImage(image, width: widthx, height: heightx)
            
            print("横長")
            let cropCGImageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(image.size.width/2 - image.size.height/2, 0, image.size.height, image.size.height))
            
            return UIImage(CGImage: cropCGImageRef!)
            
        }else if(width <= height){
            let scrsize = self.view.bounds.width
            let scale = scrsize / width
            let widthx: Int = Int(width * scale)
            let heightx: Int = Int(height * scale)
            
            
            let image = resizeImage(image, width: widthx, height: heightx)
            // 縦長
            print("縦長")
            let cropCGImageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, image.size.width, image.size.width))
            
            return UIImage(CGImage: cropCGImageRef!)
            
        }else{
            return image
        }
    }
    
    func resizeImage(image: UIImage, width: Int, height: Int) -> UIImage {
        let ref: CGImageRef = image.CGImage!
        var srcWidth: Int = CGImageGetWidth(ref)
        var srcHeight: Int = CGImageGetHeight(ref)
        
        var size: CGSize = CGSize(width: width, height: height)
        UIGraphicsBeginImageContext(size)
        image.drawInRect(CGRectMake(0, 0, size.width, size.height))
        
        let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return resizeImage
    }
    
２、一般的なPickerViewの利用にトリミング処理を加える。
    
    // カメラロールから写真を選ぶためのメソッド
    func accessCameraroll() {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            let controller = UIImagePickerController()
            controller.delegate = self
            controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }
    
    //カメラで写真を取る
    func pickImageFromCamera() {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
            let controller = UIImagePickerController()
            controller.delegate = self
            controller.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(controller, animated: true, completion: nil)
        }
        
        
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
        
    {
        /// 画像ファイルの保存先パスを生成します（ドキュメントフォルダ直下固定）。
        // ドキュメントパス
        //let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        //documentsPath.stringByAppendingString("img1.jpg")
        //let image: UIImage? = info[UIImagePickerControllerOriginalImage] as! UIImage?
        //let data = UIImageJPEGRepresentation(image!, 0.9)
        //print(documentsPath+"/test.jpg")
        //data!.writeToFile(documentsPath+"/test.jpg", atomically: true)
        if(cameraSw==0){
            let pictURL: NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL
            picurl = pictURL.absoluteString
            
            //正方形に切り取る
            ImageView.image = self.cropImageToSquare((info[UIImagePickerControllerOriginalImage] as? UIImage)!)
        }else{
            
            //正方形に切り取る
            self.ImageView.image = self.cropImageToSquare((info[UIImagePickerControllerOriginalImage] as? UIImage)!)
            
            //ImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        }
        
        
        self.dismissViewControllerAnimated(true, completion: nil)
        
        
    }