【发布时间】:2016-10-11 22:17:07
【问题描述】:
我有两个图像视图,一个带有图像,另一个带有使用 CGContext 方法定义的图像,两者都具有相同的图像大小和图像视图大小,彼此叠加。在情节提要中,我可以将两个图像视图都设置为“Aspect Fit”,这样不同设备上的用户仍然可以看到图像。但是,当我在覆盖的第二个图像视图上绘制某些东西时,它不会相应地缩放它(或相对于第一个图像视图,即使它们的大小相同)。如何使重叠图像视图中的第二张图像与下图具有相同的比例?
示例代码:
import CoreGraphics
import UIKit
class Map: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var drawnImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.minimumZoomScale = 1.0
self.scrollView.maximumZoomScale = 6.0
let data = grabData()
print(data!)
var img = UIImage(named: "floor1")
print(img!.size)
imageView.image = img
img = draw(imageView.bounds.size, data: data!)
print(img!.size)
drawnImageView.image = img
for c in drawnImageView.constraints {
if c.identifier == "constraintImageHeight" {
c.constant = img!.size.height * drawnImageView.bounds.width / img!.size.width;
break;
}
}
}
}
func draw(img: UIImage) -> UIImage {
UIGraphicsBeginImageContext(img.size)
let context = UIGraphicsGetCurrentContext()
// Drawing
let color = UIColor(red: 0.67, green: 0.4, blue: 0.56, alpha: 1)
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetLineWidth(context, 2.0)
var y = 0
for _ in 0..<100 {
let b = CGRect(origin: CGPoint(x: 0, y: y), size: CGSize(width: 1200, height: 1))
CGContextStrokeRect(context, b)
y += 30
}
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
更新 1:
(在'//绘图'之后替换)
如果您使用 iPhone 5 模拟器加载,它不会像在 iPhone 6 模拟器中那样显示在与照片相关的相同位置。
func draw(size: CGSize, data: [TotalLine]) -> UIImage {
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
let screen: CGRect = UIScreen.mainScreen().bounds
let x = screen.size.width, y = screen.size.height
// Drawing
//let color = UIColor(red: 0.67, green: 0.4, blue: 0.56, alpha: 1)
let color = UIColor.redColor()
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetLineWidth(context, 2.0)
let line = CGRect(origin: CGPoint(x: (x/16), y: (y*0.502)), size: CGSize(width: (x/20), height: 2))
CGContextStrokeRect(context, line)
let test = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: x, height: y))
CGContextStrokeRect(context, test)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
更新 2:
iPhone 6:
iPhone 5:
我想让那条红线出现在房间之间,就像 iPhone 6 的截图一样。在 iPhone 5 中,它略低。
更新 3:
打印图像视图:
iPhone 5:
绘制的图像视图:frame = (0 0; 600 536); 图像视图:frame = (0 0; 600 536);
iPhone 6:
绘制的图像视图: frame = (0 0; 600 536); 图像查看: frame = (0 0; 600 536);
【问题讨论】:
-
您更新的代码中的
x和y是什么?没有提供定义或初始值。您是否完全删除了for循环?与 iPhone 6 相比,iPhone 5 到底有什么问题? -
在调用
draw方法之前打印两个设备的 imageView 框架并在此处提供输出 -
已更新,更新 3
-
@alexburtnik 知道发生了什么吗?
-
我正在考虑它,但现在没有想法,如果不幸的是我无法重现。如果您有一个测试项目遇到此问题,请与我分享,我可能会修复它
标签: ios swift core-graphics cgcontext