小编ava*_*ava的帖子

重建应用程序时文档目录路径更改

我从url下载视频文件并使用以下路径将其保存在文档目录中:

  let destination: DownloadRequest.DownloadFileDestination = { _, _ in
      let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
      return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
Run Code Online (Sandbox Code Playgroud)

我的视频已下载并成功播放.但是有一个问题,当我在Xcode中重建应用程序并尝试播放我下载的最后一个视频时,视频没有显示,当我下载新视频时,这个保存并成功播放.

我已经看过每个视频包路径,它们是不同的.

1 - file:/// Users/myMac/Library/Developer/CoreSimulator/Devices/EAC2F4CE-EA09-46C0-B403-1CE9E24B6822/data/Containers/Data/Application/1D2C1F7B-E627-4898-91C1-D0AF8D5E0F1E/Documents/Downloads /pack7-1.mp4

2 - file:/// Users/myMac/Library/Developer/CoreSimulator/Devices/EAC2F4CE-EA09-46C0-B403-1CE9E24B6822/data/Containers/Data/Application/F950E9A5-C9F3-4B8C-BCF5-647EEC233CEE/Documents/Downloads /pack7-3.mp4

现在,我的问题是,当我们从App Store更新应用程序时,是否意味着重新安装?这条路是否改变了?

怎么能解决这个问题?

filepath ios nsdocumentdirectory swift

11
推荐指数
2
解决办法
5905
查看次数

获取Google地图的纬度和经度中心

我想显示全屏mapView,总是得到mapView中心的纬度和经度,并在这一点上显示标记.

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

    let lat = mapView.camera.target.latitude
    print(lat)

    let lon = mapView.camera.target.longitude
    print(lon)


    marker.position = CLLocationCoordinate2DMake(CLLocationDegrees(centerPoint.x) , CLLocationDegrees(centerPoint.y))
    marker.map = self.mapView
    returnPostionOfMapView(mapView: mapView)

  }

  func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    print("idleAt")

    //called when the map is idle

    returnPostionOfMapView(mapView: mapView)

  }

  func returnPostionOfMapView(mapView:GMSMapView){
    let geocoder = GMSGeocoder()
    let latitute = mapView.camera.target.latitude
    let longitude = mapView.camera.target.longitude




    let position = CLLocationCoordinate2DMake(latitute, longitude)
    geocoder.reverseGeocodeCoordinate(position) { response , error in
      if error != nil {
        print("GMSReverseGeocode Error: …
Run Code Online (Sandbox Code Playgroud)

google-maps ios swift

6
推荐指数
1
解决办法
6998
查看次数

Swift - 解析包含URL的字符串

如何解析这个字符串:

http://www.ha***ay.ir/pa***nt/result_false.php?error=Canceled%20By%20User

我尝试使用下面给出的代码将给定的字符串转换为字典.但我得到了这个错误:

无法读取数据,因为格式不正确.

这是我的代码:

 func webViewDidFinishLoad(_ webView: UIWebView) {

            print("finish loading")

            let yourTargetUrl = webView.request?.url?.absoluteString

            print(yourTargetUrl!)

            let parse = convertToDictionary(text: yourTargetUrl!)

        }

        func convertToDictionary(text: String) -> [String: Any]? {
            if let data = text.data(using: .utf8) {
                do {
                    return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                } catch {
                    print(error.localizedDescription)
                }
            }
            return nil
        }
Run Code Online (Sandbox Code Playgroud)

string ios swift

4
推荐指数
1
解决办法
6289
查看次数

AVMutableComposition 旋转视频文件

我正在使用 PryntTrimmerView 来修剪视频文件。有我的代码用于导出修剪视频文件并生成视频缩略图:

func prepareAssetComposition() throws {

    guard let asset = trimmerView.asset, let videoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).last else {
      return
    }
    let size = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
    print(CGSize(width: fabs(size.width), height: fabs(size.height)))

    let assetComposition = AVMutableComposition()
    let start = trimmerView.startTime?.seconds
    let end = trimmerView.endTime?.seconds
    let startTime = CMTime(seconds: Double(start ?? 0), preferredTimescale: 1000)
    let endTime = CMTime(seconds: Double(end ?? 0), preferredTimescale: 1000)
    let trackTimeRange = CMTimeRange(start: startTime, end: endTime)


    let compositionTrack = assetComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    try compositionTrack.insertTimeRange(trackTimeRange, of: videoTrack, at: kCMTimeZero)

    var …
Run Code Online (Sandbox Code Playgroud)

trim ios avmutablecomposition avassetexportsession swift

4
推荐指数
1
解决办法
2159
查看次数

为核心数据创建模型 URL

我想使用核心数据来保存和加载从服务器获取的数据:

 private lazy var managedObjectModel : NSManagedObjectModel = {
    let modelURL = NSBundle.mainBundle().URLForResource(self.manageObjectModelName, withExtension:"mom")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
Run Code Online (Sandbox Code Playgroud)

我有一个错误

意外发现为零

在这段代码中解开可选值时。而我的persistanceStoreCooardinator存储和mainQueueContext存储都为零。如何解决这个问题呢?

core-data ios swift

3
推荐指数
1
解决办法
669
查看次数

在后台更新位置

我想获得用户位置,即使用户没有使用app.now我可以按下主页按钮后获得位置,应用程序进入后台状态,但经过几秒钟的位置更新后停止.当我针对stoped杀死应用程序位置更新时.这是我在app delegate中的代码.

 let locationManager = CLLocationManager()
  var LatitudeGPS = String()
  var LongitudeGPS = String()
  var speedGPS = String()
  var Course = String()
  var Altitude = String()
  var bgtimer = Timer()

func applicationDidEnterBackground(_ application: UIApplication) {

    self.doBackgroundTask()
  }

  func beginBackgroundUpdateTask() {

    backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
      self.endBackgroundUpdateTask()
    })

  }

  func endBackgroundUpdateTask() {
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
  }
func doBackgroundTask() {

  DispatchQueue.global(qos: .background).async {

    self.beginBackgroundUpdateTask()
    self.StartupdateLocation()
    self.bgtimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.bgtimer(timer:)), userInfo: nil, repeats: true)
    RunLoop.current.add(self.bgtimer, forMode: RunLoopMode.defaultRunLoopMode)
    RunLoop.current.run()
    self.endBackgroundUpdateTask() …
Run Code Online (Sandbox Code Playgroud)

ios swift location-updates

3
推荐指数
1
解决办法
6434
查看次数

在文本字段中键入时在文本字段字符之间添加空格

我有一个textfield最大字符范围16,每4个字符后,我想添加减去字符或空格然后写其余的字符像这样的样本5022-2222-2222-2222.有我的代码,但这不起作用,怎么办呢?

if textField.text?.characters.count  == 5 {

               let  l = textField.text?.characters.count
            let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
            attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
            cartNumberTextField.attributedText = attributedString

            }

            else if textField.text?.characters.count  == 9 {

                let  l = textField.text?.characters.count
                let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
                attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
                cartNumberTextField.attributedText = attributedString

            }

            else if textField.text?.characters.count  == 13 {

                let  l = textField.text?.characters.count
                let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
                attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
                cartNumberTextField.attributedText …
Run Code Online (Sandbox Code Playgroud)

uitextfield ios swift

2
推荐指数
2
解决办法
6669
查看次数

取消一个带有特定 url 的 Alamofire 下载请求

我有为每个单元格下载视频文件的表格视图。这是我下载视频文件的代码。

 func beginItemDownload(id:String,url:String,selectRow:Int) {

    let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
      return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    let url = URL(string:url)

    Alamofire.download(
      url!,
      method: .get,
      parameters: nil,
      encoding: JSONEncoding.default,
      headers: nil,
      to: destination).downloadProgress(closure: { (progress) in
        DispatchQueue.main.async {

          if progress.fractionCompleted < 1 {

             print(Float(progress.fractionCompleted))

          }

          if progress.fractionCompleted == 1 {
            print("Completed")
          }
        }

      }).response(completionHandler: …
Run Code Online (Sandbox Code Playgroud)

ios swift alamofire

0
推荐指数
1
解决办法
1549
查看次数