我一直在努力调整图像大小。基本上,我遇到了以下问题: 如何缩小UIImage并使其同时变得清晰 / 锐利而不是模糊的?
这似乎是一个合适的解决方案,但不知何故它无法正常工作。
我的应用程序使用来自相机胶卷的照片。 这些照片应该调整为约200x200的尺寸,其中宽度很重要,而高度不重要。
不幸的是,由于我的愤怒导致无法工作的解决方案,我没有示例代码,抱歉。
我一直在努力调整图像大小。基本上,我遇到了以下问题: 如何缩小UIImage并使其同时变得清晰 / 锐利而不是模糊的?
这似乎是一个合适的解决方案,但不知何故它无法正常工作。
我的应用程序使用来自相机胶卷的照片。 这些照片应该调整为约200x200的尺寸,其中宽度很重要,而高度不重要。
不幸的是,由于我的愤怒导致无法工作的解决方案,我没有示例代码,抱歉。
这是我的代码。图片宽度为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)
}
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!
}`
该函数可以调整图像的大小,并返回一个新的 UIImage 对象。新的宽度是通过传入参数中的 newWidth 指定的,而高度将根据比例自动计算。函数使用了 UIKit 框架中的 UIGraphicsBeginImageContext 函数来创建一个基于位图的上下文,然后在此上下文中绘制图像并生成新的图像。最后,用 UIGraphicsEndImageContext 函数结束上下文并返回新的图像对象。 - mikey9基于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图像,那么接受的答案函数实际上会将透明区域转换为黑色。
如果你希望在缩放图像的同时保留透明度,请尝试使用这个函数:
SWIFT 4
extension UIImage {
func scaleImage(toSize newSize: CGSize) -> UIImage? {
var newImage: UIImage?
let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
if let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage {
context.interpolationQuality = .high
let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height)
context.concatenate(flipVertical)
context.draw(cgImage, in: newRect)
if let img = context.makeImage() {
newImage = UIImage(cgImage: img)
}
UIGraphicsEndImageContext()
}
return newImage
}
}
let scale = UIScreen.main.scale let newImage = UIImage(cgImage: context.makeImage()!, scale: scale, orientation: .up)
。 - picciano适用于Swift 3.0
只需将此代码片段作为扩展添加到UIImage
即可。但是,请记住,这不会使图像成为正方形,而是如果它本来就是正方形,则结果将是正方形。
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!
} }
Swift 4.0 -
如果您处理的图像包含透明度,则接受的答案函数实际上将透明区域转换为黑色。
如果您希望缩放并保留透明度,请尝试使用此函数:
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
}
这段代码使用了 iOS 10 引入的 UIGraphicsImageRenderer:在我的测试中,它比先前使用 UIGraphicsBeginImageContext(Swift 4 / Xcode 9)的示例快 10-40%:
extension UIImage {
func renderResizedImage (newWidth: CGFloat) -> UIImage {
let scale = newWidth / self.size.width
let newHeight = self.size.height * scale
let newSize = CGSize(width: newWidth, height: newHeight)
let renderer = UIGraphicsImageRenderer(size: newSize)
let image = renderer.image { (context) in
self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
}
return image
}
}
这个函数会返回一个宽度为您指定值的图片:
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)
}
这段代码在正方形图片上表现出色,并且不会失去质量。
extension UIImage {
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
}
来源回答: 如何在不损失质量的情况下调整图像大小?
在 Swift 4.2 中,进一步改进 @rommex 的答案,使用最大尺寸:
private extension UIImage {
func scaled(to maxSize: CGFloat) -> UIImage? {
let aspectRatio: CGFloat = min(maxSize / size.width, maxSize / size.height)
let newSize = CGSize(width: size.width * aspectRatio, height: size.height * aspectRatio)
let renderer = UIGraphicsImageRenderer(size: newSize)
return renderer.image { context in
draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
}
}
}
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) ")