1

在我的应用程序中,我需要在服务器上上传照片,所以在此之前,我想调整大小并将它们压缩到可接受的大小。我试图调整它们的大小有两种方式,而第一种方式是:如何在UIImage调整大小时提高清晰度?

// image is an instance of original UIImage that I want to resize 
    let width : Int = 640 
    let height : Int = 640 
    let bitsPerComponent = CGImageGetBitsPerComponent(image.CGImage) 
    let bytesPerRow = CGImageGetBytesPerRow(image.CGImage) 
    let colorSpace = CGImageGetColorSpace(image.CGImage) 
    let bitmapInfo = CGImageGetBitmapInfo(image.CGImage) 

    let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo) 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh) 
    CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(width), height: CGFloat(height))), image.CGImage) 

    image = UIImage(CGImage: CGBitmapContextCreateImage(context)) 

另一种方式:

image = RBResizeImage(image, targetSize: CGSizeMake(640, 640)) 

func RBResizeImage(image: UIImage?, targetSize: CGSize) -> UIImage? { 
     if let image = image { 
      let size = image.size 

      let widthRatio = targetSize.width/image.size.width 
      let heightRatio = targetSize.height/image.size.height 

      // Figure out what our orientation is, and use that to form the rectangle 
      var newSize: CGSize 
      if(widthRatio > heightRatio) { 
       newSize = CGSizeMake(size.width heightRatio, size.height heightRatio) 
      } else { 
       newSize = CGSizeMake(size.width widthRatio, size.height widthRatio) 
      } 

      // This is the rect that we've calculated out and this is what is actually used below 
      let rect = CGRectMake(0, 0, newSize.width, newSize.height) 

      // Actually do the resizing to the rect using the ImageContext stuff 
      UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) 
      image.drawInRect(rect) 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return newImage 
     } else { 
      return nil 
     } 
    } 

在那之后,我用UIImageJPEGRepresentation压缩UIImage的,但即使compressionQuality是1,照片仍然模糊不清(这在物体边缘大多可见,也许这不是什么大问题,但照片比Instagram上的同一张照片大三至五倍,例如但不具有相同的清晰度)。当然,对于0.5而言,情况更糟,照片的尺寸仍然大于Instagram的相同照片。

Photo from my app, compressionQuality is 1, edges are blurry, and size is 341 KB 照片来自我的应用程序,compressionQuality设置为1时,边缘模糊,大小为341 KB

Photo from Instagram, edges are sharp, and size is 136 KB 照片来自Instagram的,边缘是锋利的,并且尺寸是136 KB

编辑:

好的,但我现在有点困惑,我不知道该怎么做,以 保持纵横比?这是我如何裁剪图像(scrollView具有UIImageView,所以我可以移动和缩放图像,并在最后,我能够裁剪scrollView的可见部分是sqare)。无论如何,从上面的图像最初是2048x2048,但它仍然模糊。

var scale = 1/scrollView.zoomScale 
var visibleRect : CGRect = CGRect() 

visibleRect.origin.x = scrollView.contentOffset.x * scale 
visibleRect.origin.y = scrollView.contentOffset.y * scale 
visibleRect.size.width = scrollView.bounds.size.width * scale 
visibleRect.size.height = scrollView.bounds.size.height * scale 
image = crop(image!, rect: visibleRect) 


func crop(srcImage : UIImage, rect : CGRect) -> UIImage? { 
    var imageRef = CGImageCreateWithImageInRect(srcImage.CGImage, rect) 
    var cropped = UIImage(CGImage: imageRef) 

    return cropped 
} 
+0

HTTP:/ /stackoverflow.com/questions/28809355/resize-uiimage-not-give-me-exact-new-size-after-resize-it/28809573#28809573 – user4261201

+0

我也试过,但我得到了相同的结果。上面的照片再次是341 KB,并且每个像素都保持不变。 – Marko

回答

0

你给出的代码是赖特,但问题是你不保持图像
的长宽比为你的代码,如果你给出的相同的图像创建一个新的矩形作为

let rect = CGRectMake(0, 0, newSize.width, newSize.height) 

高度和宽度,它会给光滑的尺寸图像,但如果高度和宽度不同,你的图像模糊。所以要尽量保持纵横比
回复编辑问题
化妆高度或裁剪图像的宽度恒定
例如,如果你让宽度为常数大于使用以下代码

visibleRect.size.height = orignalImg.size.height * visibleRect.size.width/orignalImg.size.width  

image = crop(image!, rect: visibleRect) 
+0

其实我是这么做的,只是没有编写那部分代码。我总是裁剪图像,所以如果我有3264x2448图像,我裁剪到2448x2448,然后再调整到640x640。 – Marko

+0

也有同样的宽高比问题,因为您的给定图像的尺寸3264 * 2448的长宽比为1.33,当您将其裁切为2448 * 2448时,其长宽比将变为1 –

+0

只需打开此链接并查看照片即可了解什么是丢失http://en.wikipedia.org/wiki/Cropping_(image) –

相关问题