2016-04-19 3 views
0

の品質:私はこのFUNCで様々な画像をリサイズしていますUIImage

func resizeImage(image: UIImage) -> UIImage { 
     var actualHeight: CGFloat = image.size.height 
     var actualWidth: CGFloat = image.size.width 

     let maxHeight: CGFloat = 600.0 
     let maxWidth: CGFloat = text.frame.size.width - 10 

     var imgRatio: CGFloat = actualWidth/actualHeight 
     let maxRatio: CGFloat = maxWidth/maxHeight 
     let compressionQuality: CGFloat = 0.5 
     //50 percent compression 
     if actualHeight > maxHeight || actualWidth > maxWidth { 
      if imgRatio < maxRatio { 
       //adjust width according to maxHeight 
       imgRatio = maxHeight/actualHeight 
       actualWidth = imgRatio * actualWidth 
       actualHeight = maxHeight 
      } 
      else if imgRatio > maxRatio { 
       //adjust height according to maxWidth 
       imgRatio = maxWidth/actualWidth 
       actualHeight = imgRatio * actualHeight 
       actualWidth = maxWidth 
      } 
      else { 
       actualHeight = maxHeight 
       actualWidth = maxWidth 
      } 
     } 
     let rect: CGRect = CGRectMake(0.0, 0.0, actualWidth, actualHeight) 
     UIGraphicsBeginImageContext(rect.size) 
     image.drawInRect(rect) 
     let img: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     let imageData: NSData = UIImageJPEGRepresentation(img, compressionQuality)! 
     UIGraphicsEndImageContext() 

     return UIImage(data: imageData)! 
    } 

しかし、品質はちょうどひどいです....私が怒鳴る何を得るの相続人は絵:

enter image description here

私は、圧縮品質が何であったかで画像の質が変わったと思った...?上記のコードからわかるように0.5になりましたが、1に変更しても品質はまだひどいですか?

アイデア、

多くのありがとう。

+0

0.5圧縮率は元のサイズに合わせて、ぼかし効果があるようですか? – zcui93

+0

@ zcui93あなたが何を意味するのかわかりません –

+0

あなたのコードはokと思われます。質問に同じイメージの元のバージョンと変更されたバージョンを追加できますか? – Avt

答えて

2

代わりにUIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)を使用してください。あなたのデバイスに正しいスケールを使用します。使用しているバージョン(オプションなし)では、デフォルトの倍率が1.0です。 ...WithOptions()の変形を使用し、縮尺に対して0.0を渡すと、デフォルトでデバイスのネイティブスケールになります。これはあなたが望むものではないかもしれないことに注意してください。

第2に、AVMakeRectWithAspectRatioInsideRect()を使用してターゲットサイズを計算することができます。第三に、JPEGエンコーディングを行う必要はありません。生成されたイメージを直接返すことができます。

複合、これは大幅にコードを簡素化:

func resizeImage(image: UIImage) -> UIImage { 
    let maxSize = CGSize(
    width: text.frame.size.width - 10, 
    height: 600.0 
) 
    let newSize = AVMakeRectWithAspectRatioInsideRect(
    image.size, 
    CGRect(origin: CGPointZero, size: maxSize) 
).size 
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
    image.drawInRect(CGRect(origin: CGPointZero, size: newSize)) 
    let scaled = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return scaled 
} 

EDIT:これは私がプロジェクトで使用UIImageに拡張したものです:

extension UIImage { 
    /** 
    Returns an scaled version of self that fits within the provided bounding box. 
    It retains aspect ratio, and also retains the rendering mode. 
    - parameter size: The target size, in point 
    - parameter scale: Optional target scale. If omitted, uses the scale of input image 
    - returns: A scaled image 
    */ 
    func imageFittingSize(size: CGSize, scale: CGFloat? = nil) -> UIImage { 
     let newSize = AVMakeRectWithAspectRatioInsideRect(self.size, CGRect(origin: CGPointZero, size: size)).size 
     UIGraphicsBeginImageContextWithOptions(newSize, false, scale ?? self.scale) 
     self.drawInRect(CGRect(origin: CGPointZero, size: newSize)) 
     let scaled = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return scaled.imageWithRenderingMode(renderingMode) 
    } 
} 

あなたのようなので、画像の上にそれを呼び出すことができます:

let maxSize = CGSize(width: text.frame.size.width - 10, height: 600.0) 
let scaled = image.imageFittingSize(maxSize) 
+0

こんにちは感謝します、私はAVFoundationフレームワークをインストールする必要がありますか? –

+0

いいえ、iOS SDKには含まれていますが、ファイルの先頭にインポートする必要があります。 –

+0

品質は良く見えますが、正しいサイズではありません。http://i.stack.imgur.com/pIjNW.png –

関連する問題