Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 旋转后的图像会扭曲和模糊吗?_Ios_Iphone_Swift_Core Graphics - Fatal编程技术网

Ios 旋转后的图像会扭曲和模糊吗?

Ios 旋转后的图像会扭曲和模糊吗?,ios,iphone,swift,core-graphics,Ios,Iphone,Swift,Core Graphics,我使用图像视图: @IBOutlet weak var imageView: UIImageView! 绘制一个图像和另一个已旋转的图像。结果表明,旋转后的图像质量很差。在下图中,黄色框中的眼镜未旋转。红色框中的眼镜旋转4.39度 下面是我用来画眼镜的代码: UIGraphicsBeginImageContext(imageView.image!.size) imageView.image!.drawInRect(CGRectMake(0, 0, imageView.image!.s

我使用图像视图:

@IBOutlet weak var imageView: UIImageView!
绘制一个图像和另一个已旋转的图像。结果表明,旋转后的图像质量很差。在下图中,黄色框中的眼镜未旋转。红色框中的眼镜旋转4.39度

下面是我用来画眼镜的代码:

UIGraphicsBeginImageContext(imageView.image!.size)
    imageView.image!.drawInRect(CGRectMake(0, 0, imageView.image!.size.width, imageView.image!.size.height))

var drawCtxt = UIGraphicsGetCurrentContext()

var glassImage = UIImage(named: "glasses.png")
let yellowRect = CGRect(...)

CGContextSetStrokeColorWithColor(drawCtxt, UIColor.yellowColor().CGColor)
CGContextStrokeRect(drawCtxt, yellowRect)
CGContextDrawImage(drawCtxt, yellowRect, glassImage!.CGImage)

// paint the rotated glasses in the red square
CGContextSaveGState(drawCtxt)
CGContextTranslateCTM(drawCtxt, centerX, centerY)
CGContextRotateCTM(drawCtxt, 4.398 * CGFloat(M_PI) / 180)
var newRect = yellowRect
newRect.origin.x = -newRect.size.width / 2
newRect.origin.y = -newRect.size.height / 2
CGContextAddRect(drawCtxt, newRect)
CGContextSetStrokeColorWithColor(drawCtxt, UIColor.redColor().CGColor)
CGContextSetLineWidth(drawCtxt, 1)

// draw the red rect
CGContextStrokeRect(drawCtxt, newRect)
// draw the image
CGContextDrawImage(drawCtxt, newRect, glassImage!.CGImage)
CGContextRestoreGState(drawCtxt)
如何旋转和绘制眼镜而不丢失质量或获得扭曲的图像?

您应该使用创建初始上下文。将
0.0
作为
比例传递将默认为当前屏幕的比例(例如,iPhone 6上的
2.0
和iPhone 6 Plus上的
3.0

请参见
UIGraphicsBeginImageContext()
上的此注释:

此函数相当于调用UIGraphicsBeginImageContextWithOptions函数,不透明参数设置为否,比例因子为1.0


正如其他人所指出的,您需要设置上下文以允许视网膜显示

除此之外,您可能希望使用大于目标显示大小的源图像并将其缩小。(目标图像像素尺寸的2倍是一个很好的起点。)

旋转到奇数角是破坏性的。图形引擎必须将源像素的网格映射到不同的网格上,这些网格没有对齐。源图像中的完美直线在目标图像中不再是直线,等等。图形引擎必须进行一些插值,并且源像素可能分布在目标图像中的几个像素上,或者小于完整像素


通过提供更大的源图像,可以为图形引擎提供更多的信息。它可以更好地将这些源像素分割成目标像素网格。

如果使用
UIGraphicsBeginImageContextWithOptions(…,0.0)创建上下文,是否有帮助
获取与您的设备使用相同比例因子的绘图上下文?@MartinR生成上下文应该使用哪种大小?不透明的
参数做什么?我没有看到任何差异。Docs:“如果您知道位图是完全不透明的,请指定
YES
忽略alpha通道并优化位图的存储。指定
NO
意味着位图必须包含alpha通道以处理任何部分透明的像素。”