zl程序教程

您现在的位置是:首页 >  其他

当前栏目

规模下降非常大尺寸图像无失真和质量损失非常小尺寸图像

2023-04-18 12:53:02 时间

我收到了来自后端的形象是大尺寸的,因为我必须把相同的图像档案图片,并显示在同一图像底部酒吧在大小30x30的标签栏中。我试图以各种方式缩小图像,但没有任何工作。规模下降非常大尺寸图像无失真和质量损失非常小尺寸图像

试过(被模糊显示图像和扭曲的),它也没有工作Alamofire的方法:

func resizeImageWithoutDistortion(image: UIImage, size : CGSize) -> UIImage{ 


    // 1. Scale image to size disregarding aspect ratio 
    let scaledImage = image.af_imageScaled(to: size) 

    // 2. Scale image to fit within specified size while maintaining aspect ratio 
    let aspectScaledToFitImage = image.af_imageAspectScaled(toFit: size) 

    // 3. Scale image to fill specified size while maintaining aspect ratio 
    let aspectScaledToFillImage = image.af_imageAspectScaled(toFill: size) 

    return scaledImage.roundImage() 
} 

也试过如下这也没有工作:

func resizeImage(_ newWidth: CGFloat) -> UIImage { 

     let ratio = size.width/size.height 
     if ratio > 1 { 
      UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newWidth)) 
      draw(in: CGRect(x: newWidth * ratio/2 - newWidth, y: 0, width: newWidth * ratio, height: newWidth)) 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return newImage!.roundImage() 
     } else { 
      UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newWidth)) 
      draw(in: CGRect(x: 0, y: (newWidth/ratio - newWidth)/2 * (-1), width: newWidth, height: newWidth/ratio)) 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return newImage!.roundImage() 
     } 
    } 

在底部的图像截图非常失真。 enter image description here

Ishika

请与你的意思是行不通的更新您的文章,而你是在它包括一个问题(这是一个Q&A网站,而不是V&S(模糊描述和建议网站)。您还应详细指出原始图像的样子,因为除非您以高分辨率但低细节的图像开始,否则无法缩放至30x30没有质量损失(和相关的失真,例如通过描述高分辨率图像从原来的30x30图像被炸毁。 –

嗨,我已经添加了截图和al l我想要的是保持图像的质量在个人资料图片和底部栏。 –

我认为UIImage应该自己做。只是不要调整图像大小。让它保持那么大,并使用方面填充。为了使图像轮回使用UIImage的图层并绕过它,而不是真正处理图像。这是一个非常昂贵的操作。确保在该图像视图上将纵横比设置为1:1。 –

回答

Ishika,你的问题是不是质量损失。你的问题是你不考虑iOS Scaling。

点和像素是同样的事情。

如果您有W: 30H: 30(点)的的UIImageView来计算您在像素的图像清晰地显示出它在不影响质量,你需要有一个形象像素尺寸

规模下降非常大尺寸图像无失真和质量损失非常小尺寸图像