iOS Swift:将图像大小调整为200x200pt / px

我一直在努力调整图像的大小。 基本上我已经st然心动了: 如何缩小UIImage,同时使其变得脆弱/锐利而不是模糊?

这似乎是一个合法的解决scheme,但它不能正常工作。

我的应用程序适用于相机胶卷中的照片。 这张照片应该调整到大约200×200,而宽度是重要的,而不是高度。

不幸的是,我没有一个示例代码,因为我放弃了我的愤怒关于非工作解决scheme;(对不起!

谢谢您的回答!

这是我的代码。 该图像宽度为850像素,而不是200像素:

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 imagenow = info[UIImagePickerControllerOriginalImage] as? UIImage imageImage.image = resizeImage(imagenow!, newWidth: 200) pimg2 = imageImage.image! cidnew2 = textFieldCID!.text! pname2 = textFieldName!.text pmanu2 = textFieldMan!.text pnick2 = textFieldNick!.text podate2 = textFieldPODate!.text pno2 = textFieldArtNo!.text self.dismissViewControllerAnimated(true, completion: nil) } 

根据swift_dan的回答,Swift 3的更新

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

如果您正在处理包含透明胶片的PNG图像,则接受的答案function实际上会将透明区域转换为黑色。

如果您想缩放并保持透明胶片,请尝试以下function:

SWIFT 3

  extension UIImage { func scaleImage(toSize newSize: CGSize) -> UIImage? { let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral UIGraphicsBeginImageContextWithOptions(newSize, false, 0) if let context = UIGraphicsGetCurrentContext() { context.interpolationQuality = .high let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height) context.concatenate(flipVertical) context.draw(self.cgImage!, in: newRect) let newImage = UIImage(cgImage: context.makeImage()!) UIGraphicsEndImageContext() return newImage } return nil } } 

对于Swift 3.0

只需将此片段作为扩展名添加到UIImage 。 但是,请记住,不是要把图像做成方形,而是如果是这种forms,结果将是方形的。

 extension UIImage { func resizeImage(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! } } 

这个函数将返回一个你指定的宽度的图像:

 func scaleImage(image: UIImage, maximumWidth: CGFloat) -> UIImage { let rect: CGRect = CGRectMake(0, 0, image.size.width, image.size.height) let cgImage: CGImageRef = CGImageCreateWithImageInRect(image.CGImage!, rect)! return UIImage(CGImage: cgImage, scale: image.size.width / maximumWidth, orientation: image.imageOrientation) } 

Swift 3.0

 func scaledImage(_ image: UIImage, maximumWidth: CGFloat) -> UIImage { let rect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) let cgImage: CGImage = image.cgImage!.cropping(to: rect)! return UIImage(cgImage: cgImage, scale: image.size.width / maximumWidth, orientation: image.imageOrientation) } 

如果你使用翠鸟lib来加载你的项目中的图像,并想调整它的大小,方法是:

  • Xcode 8
  • Swift 3x
  •  let imageUrl = URL(string: "your image url") //Size refer to the size which you want to resize your original image let size = CGSize(width: 60, height: 60) let processImage = ResizingImageProcessor(targetSize: size, contentMode: .aspectFit) cell.courseTitleImage.kf.setImage(with: imageUrl! , placeholder: UIImage(named: "placeholder"), options: [.transition(ImageTransition.fade(1)), .processor(processImage)], progressBlock: nil, completionHandler: nil) 

    要么

    调整本地图像大小: – 您可以参考@Christoph R的答案

    Swift 4.0 –

    如果您正在处理包含透明胶片的图像,则接受的答案function实际上会将透明区域转换为黑色。

    如果您想缩放并保持透明胶片,请尝试以下function:

     func resizeImageWith(image: UIImage, newSize: CGSize) -> UIImage { let horizontalRatio = newSize.width / image.size.width let verticalRatio = newSize.height / image.size.height let ratio = max(horizontalRatio, verticalRatio) let newSize = CGSize(width: image.size.width * ratio, height: image.size.height * ratio) var newImage: UIImage if #available(iOS 10.0, *) { let renderFormat = UIGraphicsImageRendererFormat.default() renderFormat.opaque = false let renderer = UIGraphicsImageRenderer(size: CGSize(width: newSize.width, height: newSize.height), format: renderFormat) newImage = renderer.image { (context) in image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) } } else { UIGraphicsBeginImageContextWithOptions(CGSize(width: newSize.width, height: newSize.height), isOpaque, 0) image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) newImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() } return newImage } 
     func getScaledDimension(width: CGFloat, height: CGFloat,new_width: CGFloat, new_height: CGFloat)->CGPoint { let widthAspect = (width / new_width) let heightAspect = (height / new_height) if widthAspect == 0 || heightAspect == 0 { return CGPoint(x: width, y: height) } var width1 : CGFloat = 0 var height1 : CGFloat = 0 if widthAspect > heightAspect { width1 = (width) / heightAspect height1 = (height) / heightAspect } else { width1 = (width) / widthAspect height1 = (height) / widthAspect } return CGPoint(x: width1, y: height1 ) } func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage { let rect = CGRectMake(0, 0, targetSize.width, targetSize.height) UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0) image.drawInRect(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } let imagesize = getScaledDimension(image.size.width, height: image.size.height , new_width: Width, new_height: Hieght) print("Image Size Scaled Dimension -> H:\(imagesize.x) W:\(imagesize.y)") let newImage = ResizeImage(image, targetSize: CGSizeMake(imagesize.x,imagesize.y)) print("Resize Image Size -> H\(newImage.size.height) W\(newImage.size.width) ") 

    将图像大小减less1024,您可以随时根据服务器容量进行转换

    func resizeImage(image:UIImage) – > UIImage {

      if image.size.height >= 1024 && image.size.width >= 1024 { UIGraphicsBeginImageContext(CGSize(width:1024, height:1024)) image.draw(in: CGRect(x:0, y:0, width:1024, height:1024)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else if image.size.height >= 1024 && image.size.width < 1024 { UIGraphicsBeginImageContext(CGSize(width:image.size.width, height:1024)) image.draw(in: CGRect(x:0, y:0, width:image.size.width, height:1024)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else if image.size.width >= 1024 && image.size.height < 1024 { UIGraphicsBeginImageContext(CGSize(width:1024, height:image.size.height)) image.draw(in: CGRect(x:0, y:0, width:1024, height:image.size.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else { return image } }