ios – Swift:UIGraphicsBeginImageContextWithOptions比例因子设置为0但未应用

ios – Swift:UIGraphicsBeginImageContextWithOptions比例因子设置为0但未应用,第1张

概述我曾经使用以下代码调整图像大小,并且它曾经在比例因子上工作得很好.现在使用 Swift 3我无法弄清楚为什么不考虑比例因子.调整图像大小但未应用比例因子.你知道为什么吗? let layer = self.imageview.layerUIGraphicsBeginImageContextWithOptions(layer.bounds.size, true, 0)layer.render 我曾经使用以下代码调整图像大小,并且它曾经在比例因子上工作得很好.现在使用 Swift 3我无法弄清楚为什么不考虑比例因子.调整图像大小但未应用比例因子.你知道为什么吗?

let layer = self.imagevIEw.layerUIGraphicsBeginImageContextWithOptions(layer.bounds.size,true,0)layer.render(in: UIGraphicsGetCurrentContext()!)let scaledImage = UIGraphicsGetimageFromCurrentimageContext()UIGraphicsEndImageContext()print("SCALED IMAGE SIZE IS \(scaledImage!.size)")print(scaledImage!.scale)

例如,如果我在iPhone 5上截取屏幕截图,图像大小将为320 * 568.我以前用相同的代码得到640 * 1136 ..什么可能导致比例因子不被应用?

当我打印图像的比例时,它将根据设备分辨率打印1,2或3,但不会应用于从上下文拍摄的图像.

解决方法 scaledImage!.size不会以像素为单位返回图像大小.

CGImageGetWIDth和CGImageGetHeight返回相同的大小(以像素为单位)
那就是image.size * image.scale

如果你想测试它,首先你必须导入CoreGraphics

let imageSize = scaledImage!.size //(320,568)let imageWIDthInPixel = CGImageGetWIDth(scaledImage as! CGImage) //640let imageHeightInPixel = CGImageGetHeight(scaledImage as! CGImage) //1136
总结

以上是内存溢出为你收集整理的ios – Swift:UIGraphicsBeginImageContextWithOptions比例因子设置为0但未应用全部内容,希望文章能够帮你解决ios – Swift:UIGraphicsBeginImageContextWithOptions比例因子设置为0但未应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1062775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存