此图像由以下函数生成,拒绝显示在UIImageView
中https://www.dropbox.com/s/7f65yxsqxzqqies/soundwave.png?dl=0
生成图片时这是iOS问题还是错误?
谢谢
private func plotLogGraph(_ samples: [CGFloat], maximumValue max: CGFloat, zeroValue min: CGFloat, imageHeight: CGFloat,color:UIColor) -> UIImage? {
let imageSize = CGSize(width: CGFloat(samples.count), height: imageHeight)
UIGraphicsBeginImageContext(imageSize)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
context.setAlpha(1.0)
context.setLineWidth(1.0)
context.setStrokeColor(color.cgColor)
let sampleDrawingScale: CGFloat
if max == min {
sampleDrawingScale = 0
} else {
sampleDrawingScale = imageHeight / 2 / (max - min)
}
let verticalMiddle = imageHeight / 2
for (x, sample) in samples.enumerated() {
let height = (sample - min) * sampleDrawingScale
context.move(to: CGPoint(x: CGFloat(x), y: verticalMiddle - height))
context.addLine(to: CGPoint(x: CGFloat(x), y: verticalMiddle + height))
context.strokePath();
}
guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
return nil;
}
UIGraphicsEndImageContext()
return image;
}
渲染示例只是显示图像的全屏ImageView,请参阅 https://github.com/jonathanort/ImageTest