【问题标题】:scale image to smaller size in swift3 [closed]在swift3中将图像缩放到更小的尺寸[关闭]
【发布时间】:2017-07-21 14:52:58
【问题描述】:

我正在尝试缩放正在以图形方式编程到照片库中的图像。我正在尝试缩放图像以使其更小。我将如何使用下面的代码使图像更小

   image1.scale

【问题讨论】:

标签: ios swift swift3 save scale


【解决方案1】:

使用这个扩展来调整你的图片大小:

extension UIImage{

func resizeImageWith(newSize: CGSize) -> UIImage {

    let horizontalRatio = newSize.width / size.width
    let verticalRatio = newSize.height / size.height

    let ratio = max(horizontalRatio, verticalRatio)
    let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
    UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
    draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
    }


}

基本上,您正在计算纵横比以在调整图像大小时保持图像完整。然后,您将获取当前图像上下文并将其绘制在指定的矩形中,最后将其作为新图像返回。

【讨论】:

  • -图像底部出现黑线。
  • 透明图像的错误解决方案
  • @user924 如果你想要一个透明的背景,你应该改变 UIGraphicsBeginImageContextWithOptions(newSize, true, 0) 到 UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
  • 如果您需要 newSize 成为图像在两个轴上的“最大尺寸”,那么您可以将let ratio = max(horizontalRatio, verticalRatio) 替换为let ratio = min(horizontalRatio, verticalRatio)。所以新的UIImage 永远不会大于newSize 的宽度或高度
【解决方案2】:

试试这个:

第一种方法:

func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {

let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}

第二种方法:

import UIKit

func RBSquareImageTo(image: UIImage, size: CGSize) -> UIImage {
  return RBResizeImage(RBSquareImage(image), size)
}

func RBSquareImage(image: UIImage) -> UIImage {
  var originalWidth  = image.size.width
  var originalHeight = image.size.height

  var edge: CGFloat
if originalWidth > originalHeight {
    edge = originalHeight
} else {
    edge = originalWidth
}

var posX = (originalWidth  - edge) / 2.0
var posY = (originalHeight - edge) / 2.0

var cropSquare = CGRectMake(posX, posY, edge, edge)

var imageRef = CGImageCreateWithImageInRect(image.CGImage, cropSquare);
return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation)
 }

func RBResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
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
  }

参考:

  1. Here

  2. Here

【讨论】:

    【解决方案3】:

    这可能会有所帮助,此方法可以按比例调整质量良好的图像。

    func resizeImage(image: UIImage, withSize: CGSize) -> UIImage {
    
        var actualHeight: CGFloat = image.size.height
        var actualWidth: CGFloat = image.size.width
        let maxHeight: CGFloat = withSize.width
        let maxWidth: CGFloat = withSize.height
        var imgRatio: CGFloat = actualWidth/actualHeight
        let maxRatio: CGFloat = maxWidth/maxHeight
        let compressionQuality = 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 = CGRect(x: 0.0, y: 0.0, width: actualWidth, height: actualHeight)
        UIGraphicsBeginImageContext(rect.size)
        image.draw(in: rect)
        let image: UIImage  = UIGraphicsGetImageFromCurrentImageContext()!
        let imageData = UIImageJPEGRepresentation(image, CGFloat(compressionQuality))
        UIGraphicsEndImageContext()
    
        let resizedImage = UIImage(data: imageData!)
        return resizedImage!
    
    }
    

    像这样调用这个方法,

    resizeImage(image: UIImage(named: "ImageName"), withSize: CGSize(width: 300, height: 300))
    

    谢谢:)

    【讨论】:

    • 嘿,很好。但是,这会为图像添加背景颜色。
    • 它只返回白色背景图片
    • 你是怎么调用这个方法的?
    • 我的图像是白色的,它有透明区域,你的解决方案删除了​​透明度(用白色替换它),所以我只得到了白色矩形
    • 你的解决方案也比stackoverflow.com/a/42546027/7767664差,图像质量不是很好,该解决方案也有透明度问题,但我在这里得到了答案stackoverflow.com/questions/48403550/…
    【解决方案4】:
     func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
    
        let scale = newWidth / image.size.width
        let newHeight = image.size.height * scale
        UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
        image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return newImage
    }
    
    
    @IBAction func chooseImage(sender: AnyObject) {
    
    
        var myPickerController = UIImagePickerController()
        myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        myPickerController.delegate = self;
        self.presentViewController(myPickerController, animated: true, completion: nil)
    
    
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
    
    {
        var image = info[UIImagePickerControllerOriginalImage] as? UIImage
        let scaledImage:UIImage = resizeImage(image!, newWidth: 200)
        self.dismissViewControllerAnimated(true, completion: nil)
    
    }
    

    Swift 4 版本

    extension UIImage {
    
        func resize(withWidth newWidth: CGFloat) -> UIImage? {
    
            let scale = newWidth / self.size.width
            let newHeight = self.size.height * scale
            UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
            self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多