如何在iOS应用程序中的相机中添加当前时间戳

6

我希望在录制视频并保存到相册时添加当前时间戳。如下图所示:

enter image description here

**注意**:我已经成功地在AVVideoPreviewLayer中添加了当前位置和时间戳,但是在导出视频后,它是静态的,不会显示实时时间戳。

2个回答

1

1

我试了一些东西。我猜你想获取纬度、经度和时间戳。

下面是纬度和经度的示例。

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func showLocation(_ sender: Any) {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if(CLLocationManager.locationServicesEnabled()){
            locationManager.startUpdatingLocation()
        }
        locationManager.stopUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
         print("Error \(error)")
    }
}

要获取当前时间,您可以使用类似以下的代码:
let date = NSDate()
print(date)

两者在控制台中的结果应该类似于这样:
2017-12-17 21:45:39 +0000
user latitude = 37.3322499
user longitude = -122.056264

在我的示例中,时间看起来是这样的,因为我来自欧盟,但您可以将其转换为您想要的格式。

您可以将此添加到单独的视图中,并使用

view.bringSubview(toFront: YourView)

我希望这对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接