首页 > 解决方案 > 为什么我的程序化屏幕截图捕捉到过时的视图?

问题描述

我有一个允许用户绘制周界线的地图视图,我需要在完成后捕获屏幕截图以保存以进行记录。出于某种原因,我的代码在添加新叠加层之前捕获了视图状态,即使我在尝试截屏之前添加了叠加层。

我使用单独的视图来捕获手势,然后将点转换为叠加层并将其添加到此处的地图中,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
}

我已经尝试了很多变体这种我在网上找到的“截图”方法,但没有运气。调试时,我可以检查地图及其叠加层,并提前查看它们是否正在更新。

任何想法为什么这里捕获的图像没有捕获所做的更改?

标签: iosswiftuikit

解决方案


推荐阅读