Ios 图像缩放和质量损失
一段时间以来,我一直试图将图像缩小到更小的尺寸,但我无法理解为什么它会失去质量,尽管我遇到过教程说它不应该这样做。首先,我将图像裁剪成正方形,然后使用以下代码:Ios 图像缩放和质量损失,ios,swift,Ios,Swift,一段时间以来,我一直试图将图像缩小到更小的尺寸,但我无法理解为什么它会失去质量,尽管我遇到过教程说它不应该这样做。首先,我将图像裁剪成正方形,然后使用以下代码: let newRect = CGRectIntegral(CGRectMake(0,0, newSize.width, newSize.height)) let imageRef = image.CGImage UIGraphicsBeginImageContextWithOptions
let newRect = CGRectIntegral(CGRectMake(0,0, newSize.width, newSize.height))
let imageRef = image.CGImage
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
let context = UIGraphicsGetCurrentContext()
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)
let flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height)
CGContextConcatCTM(context, flipVertical)
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef)
let newImageRef = CGBitmapContextCreateImage(context)! as CGImage
let newImage = UIImage(CGImage: newImageRef)
// Get the resized image from the context and a UIImage
UIGraphicsEndImageContext()
cell.ImageView.layer.cornerRadius = 30.0
cell.ImageView.clipsToBounds = true
我也尝试过这段代码,结果相同:
let newSize:CGSize = CGSize(width: 30,` height: 30)
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
UIBezierPath(
roundedRect: rect,
cornerRadius: 2
).addClip()
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageData = UIImageJPEGRepresentation(newImage, 1.0)
sharedInstance.my.append(UIImage(data: imageData!)!)
调整大小后,我仍然会看到模糊的图像。我将其与图像视图进行比较,并将其设置为aspect fill/fit,图像更清晰、更小。这就是我想要得到的品质,我不知道我错过了什么。我在这里放了两张图片,第一张是使用imageView的更清晰的图片,第二张是用我的代码调整大小的图片。如何操纵图像,使其看起来像在图像视图中一样清晰
您应该使用
let newSize:CGSize = CGSize(width: 30 * UIScreen.mainScreen().scale, height: 30 * UIScreen.mainScreen().scale)
这是因为不同的iPhone大小不同。在您显示的示例图像中,您绘制的“后”图像比起始图像大。如果你把一幅图像从较大的尺寸缩小到30像素乘以30像素,你就浪费了很多信息。如果你用更大的尺寸绘制30x30的图像,它看起来会很糟糕。30 X 30是一个很小的图像,没有太多细节。选择图像视图,单击“大小”检查器并更改“X”, “Y”、“宽度”和“高度”属性
X = 14
Y = 10
Width = 60
Height = 60
对于圆半径,可以实现以下代码:
let newRect = CGRectIntegral(CGRectMake(0,0, newSize.width, newSize.height))
let imageRef = image.CGImage
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
let context = UIGraphicsGetCurrentContext()
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)
let flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height)
CGContextConcatCTM(context, flipVertical)
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef)
let newImageRef = CGBitmapContextCreateImage(context)! as CGImage
let newImage = UIImage(CGImage: newImageRef)
// Get the resized image from the context and a UIImage
UIGraphicsEndImageContext()
cell.ImageView.layer.cornerRadius = 30.0
cell.ImageView.clipsToBounds = true
或
转到标识检查器,单击对话框左下角的添加按钮(+)
用户定义的运行时属性编辑器。
双击新属性的关键路径字段,编辑属性到层的关键路径。cornerRadius
将类型设置为Number
和
将值设置为30
。要从方形图像生成圆形图像
半径设置为图像视图宽度的一半
Duncan给了你一个很好的解释30 x 30太小了这就是为什么像素或图像质量会下降,我建议你使用60 x 60你看过如何调整图像大小的教程吗?是的,我也尝试过这个方法..我不知道问题出在哪里,特别是因为我先切到一个正方形,然后将它设置为30x30,为什么你不会损失质量呢?您正在缩小图像。可能会发生丢失,问题是您正在获取图像的JPEG,默认情况下会导致质量损失。您的新图像已经是UIImage,因此您不需要转到JPeg并返回UIImage是否使用表视图单元格?我想这就是将
0
传递到UIGraphicsBeginImageContextWithOptions
应该做的。