2016-04-29 11 views
4

UIImageを回転させるために、私はインターネットからダウンロードされた拡張子を使用しました。そこでは、回転が働いていますが、いくつかの有線の振る舞いがあります。UIImageを回転させる問題 - IOS/Swift

これは、拡張コード

import UIKit 

extension UIImage { 

    public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage { 

     let radiansToDegrees: (CGFloat) -> CGFloat = { 
      return $0 * (180.0/CGFloat(M_PI)); 
     } 
     let degreesToRadians: (CGFloat) -> CGFloat = { 
      return $0/180.0 * CGFloat(M_PI); 
     } 

     // calculate the size of the rotated view's containing box for our drawing space 
     let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size)); 
     let t = CGAffineTransformMakeRotation(degreesToRadians(degrees)); 
     rotatedViewBox.transform = t; 
     let rotatedSize = rotatedViewBox.frame.size 

     // Create the bitmap context 
     UIGraphicsBeginImageContext(rotatedSize); 
     let bitmap = UIGraphicsGetCurrentContext(); 

     // Move the origin to the middle of the image so we will rotate and scale around the center. 
     CGContextTranslateCTM(bitmap, rotatedSize.width/2.0, rotatedSize.height/2.0); 

     // Rotate the image context 
     CGContextRotateCTM(bitmap, degreesToRadians(degrees)); 

     // Now, draw the rotated/scaled image into the context 
     var yFlip: CGFloat; 

     if(flip){ 
      yFlip = CGFloat(-1.0); 
     } else { 
      yFlip = CGFloat(1.0); 
     } 

     CGContextScaleCTM(bitmap, yFlip, -1.0); 
     CGContextDrawImage(bitmap, CGRectMake(-size.width/2, -size.height/2, size.width, size.height), CGImage); 

     let newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     return newImage; 
    } 
} 

ですこれは、私がイメージビューにイメージ返さ拡張メソッドとセット呼び出す方法です。画像ビューの「contentMode」プロパティは、これは、それが回転ボタンを押す前にどのように見えるかです

currentPagePreviewImageVew.image = currentPagePreviewImageVew.image!.imageRotatedByDegrees(90, flip: false); 

「記入するスケール」に設定されている

これは、一度回転ボタンを押した後で

enter image description here

。(画像を参照してください。とき画像Rこれは回転しないが、それのサイズが変更されている)

enter image description here

otationは90度である。(画像が回転しているが、あなたが見ることができるように、他のビューが伸びます。ここ後の画像は、上記の画面のように0度と180度である(2))

enter image description here

は、いくつかの1缶私のコードに何が間違っているのか教えてください。もっと良い解決策があれば教えてください。どんな助けも高く評価されます。

編集:ビューのストレッチは制約の問題が原因でした。私はイメージビューの上のツールバーに高さの制約を置くことでそれを解決しました。しかし、初めてのことはまだ答えが見つかりませんでした。

+0

アファイン変換行列を変更すると自動レイアウト制約に影響があったため、問題が発生しました。これは同様の問題です。 – Daniel

+0

@simpleBob作業を完了するために何をしましたか? – GMHSJ

+0

オブジェクトの制約を取り除いて再度設定しました – Daniel

答えて

0

私は拡張子の下に使用し、それがうまく働きました。これは実際にswift 3です。

import UIKit 

extension UIImage { 

    public func rotateImageByDegrees(_ degrees: CGFloat) -> UIImage { 

     let degreesToRadians: (CGFloat) -> CGFloat = { 
      return $0/180.0 * CGFloat(M_PI) 
     } 

     // calculate the size of the rotated view's containing box for our drawing space 
     let rotatedViewBox = UIView(frame: CGRect(origin: CGPoint.zero, size: self.size)) 
     let t = CGAffineTransform(rotationAngle: degreesToRadians(degrees)); 
     rotatedViewBox.transform = t 
     let rotatedSize = rotatedViewBox.frame.size 

     // Create the bitmap context 
     UIGraphicsBeginImageContext(rotatedSize) 
     let bitmap = UIGraphicsGetCurrentContext() 

     // Move the origin to the middle of the image so we will rotate and scale around the center. 
     bitmap?.translateBy(x: rotatedSize.width/2.0, y: rotatedSize.height/2.0); 

     // Rotate the image context 
     bitmap?.rotate(by: degreesToRadians(degrees)); 

     // Now, draw the rotated/scaled image into the context 
     bitmap?.scaleBy(x: 1.0, y: -1.0) 
     bitmap?.draw(self.cgImage!, in: CGRect(x: -self.size.width/2, y: -self.size.height/2, width: self.size.width, height: self.size.height)) 

     let cgimage:CGImage = bitmap!.makeImage()! 
     return UIImage(cgImage: cgimage) 
    } 
} 

その他の事項はすべて質問と同じです。

+0

@LiborZapletalこれを試してみてください。それは私のために働いた – GMHSJ

+0

更新ありがとう。私は前に同様の方法を試してみました。風景写真を回転させようとすると、たとえ写真を肖像画や風景に戻しても、肖像画として写真を撮って、回転しようとするよりも、最初の回転で画像が変形してしまいます。回転はうまくいきますが、変形した写真で回転します。 –

+0

私はそれを使う方法よりもuiimageviewを持っています。 – MRizwan33

1

あなたはこの試みなければならない。その後、あなたの新しいサイズを満たすために、このメソッドを使用し

extension UIImage { 
    public func imageRotatedByDegrees(degrees: Double, flip: Bool) -> UIImage { 

     // calculate the size of the rotated view's containing box for our drawing space 
     let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size)) 
     let t = CGAffineTransformMakeRotation(CGFloat(toRadian(degrees))) 
     rotatedViewBox.transform = t 
     let rotatedSize = rotatedViewBox.frame.size 

     // Create the bitmap context 
     UIGraphicsBeginImageContext(rotatedSize) 
     let bitmap = UIGraphicsGetCurrentContext() 

     CGContextSetAllowsAntialiasing(bitmap, true) 
     CGContextSetShouldAntialias(bitmap, true) 
     CGContextSetInterpolationQuality(bitmap, CGInterpolationQuality.High) 

     // Move the origin to the middle of the image so we will rotate and scale around the center. 
     CGContextTranslateCTM(bitmap, rotatedSize.width/2.0, rotatedSize.height/2.0) 

     // // Rotate the image context 
     CGContextRotateCTM(bitmap, CGFloat(toRadian(degrees))) 

     // Now, draw the rotated/scaled image into the context 
     var yFlip: CGFloat 

     if(flip){ 
      yFlip = CGFloat(-1.0) 
     } else { 
      yFlip = CGFloat(1.0) 
     } 

     CGContextScaleCTM(bitmap, yFlip, -1.0) 
     CGContextDrawImage(bitmap, CGRectMake(-size.width/2, -size.height/2, size.width, size.height), CGImage) 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return newImage 
    } 

を:

extension UIImage { 
    public func resizeImage(newSize:CGSize) -> UIImage { 

     let newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)) 
     let imageRef:CGImageRef = self.CGImage! 

     UIGraphicsBeginImageContextWithOptions(newSize, false, 0) 
     let context:CGContextRef = UIGraphicsGetCurrentContext()! 

     // Set the quality level to use when rescaling 
     CGContextSetInterpolationQuality(context, CGInterpolationQuality.High) 
     CGContextSetAllowsAntialiasing(context, true) 
     CGContextSetShouldAntialias(context, true) 

     let flipVertical:CGAffineTransform = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height) 

     CGContextConcatCTM(context, flipVertical) 
     // Draw into the context; this scales the image 
     CGContextDrawImage(context, newRect, imageRef) 

     // Get the resized image from the context and a UIImage 
     let newImage:UIImage = UIImage(CGImage: CGBitmapContextCreateImage(context)!) 

     UIGraphicsEndImageContext(); 
     return newImage 
    } 
} 
+0

仲間を助けてくれてありがとう。しかし、これは私の問題を解決しませんでした。 – GMHSJ

4
func flipImageVertical(originalImage: UIImage) -> UIImage 
    { 
     let tempImageView:UIImageView = UIImageView(image: originalImage) 
     UIGraphicsBeginImageContext(tempImageView.frame.size) 
     let context:CGContextRef = UIGraphicsGetCurrentContext()! 
     let flipVertical:CGAffineTransform = CGAffineTransformMake(1,0,0,-1,0,tempImageView.frame.size.height) 
     CGContextConcatCTM(context, flipVertical) 
     tempImageView.layer .renderInContext(context) 

     let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return flippedImage 
    } 

    func flipImageHorizontal(originalImage: UIImage) -> UIImage 
    { 
     let tempImageView:UIImageView = UIImageView(image: originalImage) 
     UIGraphicsBeginImageContext(tempImageView.frame.size) 
     let context:CGContextRef = UIGraphicsGetCurrentContext()! 
     let flipHorizontal:CGAffineTransform = CGAffineTransformMake(-1,0,0,1,tempImageView.frame.size.width,0) 

     CGContextConcatCTM(context, flipHorizontal) 
     tempImageView.layer .renderInContext(context) 

     let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return flippedImage 
    } 
+0

あなたの両方の方法は正常に動作しています。しかし、これは私が欲しいものではありません。私はイメージを回転させたい。反転しない – GMHSJ