ios - 为什么我的程序化屏幕截图捕捉到过时的视图?
问题描述
我有一个允许用户绘制周界线的地图视图,我需要在完成后捕获屏幕截图以保存以进行记录。出于某种原因,我的代码在添加新叠加层之前捕获了视图状态,即使我在尝试截屏之前添加了叠加层。
我使用单独的视图来捕获手势,然后将点转换为叠加层并将其添加到此处的地图中,points
从手势跟踪中收集的点在哪里
func convertFragments() {
var coordinates: [CLLocationCoordinate2D] = []
for point in points {
let coordinate = mapView.convert(point, toCoordinateFrom: drawingView)
coordinates.append(coordinate)
}
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
polyline.title = selectedTool.name
removeLines(for: selectedTool)
points = []
mapView.addOverlay(polyline)
incidentManager.update(for: selectedTool, value: polyline)
}
然后incidentManager.update(value: polyline)
进行网络调用以保存信息并调用第二种方法来捕获屏幕截图并更新登录log(eventType: eventType)
func update(for tool: MapTool, value: Any) {
func saveLine(_ line: MKPolyline, for key: String) {
incidentReference.setData([
key: IncidentManager.convertCLPoints(line.coordinates)
], merge: true)
}
switch tool {
case .hotZone, .innerPerimeter, .outerPerimeter:
let line = value as! MKPolyline
saveLine(line, for: tool.rawValue)
case .commandPost, .stagingArea:
let point = value as! CLLocationCoordinate2D
let geoPoint = GeoPoint(latitude: point.latitude, longitude: point.longitude)
incidentReference.setData([
tool.rawValue: geoPoint
], merge: true)
case .poi:
let annotation = value as! PerimeterMapAnnotation
let point = annotation.coordinate
let geoPoint = GeoPoint(latitude: point.latitude, longitude: point.longitude)
incidentReference.setData([
"pointsOfInterest": [annotation.title: geoPoint]
], merge: true)
default:
return
}
updateAddress(defaultValue: value)
let eventType = tool.eventType(didSet: true)
log(eventType: eventType)
}
我有一个UIView
捕获视图的扩展,但由于某种原因,它不是地图视图的最新版本。
private func log(eventType: LogEventType) {
guard let mapImage = mapView.asImage() else { return }
let storageRef = Storage.storage().reference()
let imageRef = storageRef.child("\(incident.id)/\(UUID().uuidString).jpg")
let eventTime = Date()
let eventTimeString = eventTime.apiDateString
incidentReference.collection("eventLog").document(eventTimeString).setData([
"logEventType": eventType.rawValue,
"imageReference": imageRef.fullPath,
"date": Timestamp(date: eventTime)
])
upload(image: mapImage, at: imageRef)
if shouldUpdateCover {
shouldUpdateCover = false
incidentReference.setData([
"coverPhotoRef": imageRef.fullPath
], merge: true)
}
}
func asImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
if let context = UIGraphicsGetCurrentContext() {
self.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
return nil
}
我已经尝试了很多变体这种我在网上找到的“截图”方法,但没有运气。调试时,我可以检查地图及其叠加层,并提前查看它们是否正在更新。
任何想法为什么这里捕获的图像没有捕获所做的更改?
解决方案
推荐阅读
- javascript - removeEventListener 未删除
- karate - 使用 Multipart 字段在空手道中上传 XML 文件并获取 415 不受支持的媒体类型
- java - 如何在 Elasticsearch 6.8.x 的索引中查找无效文档,同时获取“type”:“number_format_exception”,“reason”:“对于输入字符串:\”\u00000\”
- javascript - 更新我得到的所有依赖项:元素类型无效:需要字符串或类/函数(用于复合组件)但得到:未定义
- css - 粗体字重不适用于 dompdf 中的 Google 字体
- powershell - 分配给变量时启动进程“访问被拒绝”
- stripe-payments - Stripe:获取卡信息,以便客户可以更新他们的卡
- java - 通过构造函数参数 2 表示的不满足的依赖关系
- sql - SQL查询,循环数据
- r - 在多个条件上使用匹配以在 R 中生成值