最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Swift 共享文件操作小結(jié)(iOS 8 +)

 更新時(shí)間:2016年07月22日 11:18:43   投稿:lqh  
本文主要介紹IOS 共享文件,在這里給大家展示實(shí)例代碼供大家參考,希望能幫助開(kāi)發(fā)IOS的同學(xué)

前言

  適用于 iOS 8 + 本地共享文件列表

正文

  一、準(zhǔn)備

    1.1  默認(rèn) App 的文件共享是關(guān)閉的,需要在 plist 中設(shè)置啟用:

    Application supports iTunes file sharing  設(shè)置為  YES

啟用后把設(shè)備連接到 iTunes 上,在 iTunes 應(yīng)用里的文件共享就能看到你的 App 了(如果看不見(jiàn)需要斷開(kāi)重新拔插一下數(shù)據(jù)線),可以拷貝一些視頻進(jìn)去,便于測(cè)試。

    1.2  導(dǎo)入庫(kù)

      Photos.framework

      AVKit.framework  用于播放視頻    

  二、獲取視頻列表

 private let VIDEO_EXTENSIONS = [
    ".MOV", ".MP4"
  ]

  private var fileManager = NSFileManager.defaultManager()
  
  func loadVideos() {
    var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    if paths.count > 0 {
      let documentsDirectory = paths[0] as String
      let documentUrl = NSURL(fileURLWithPath: documentsDirectory, isDirectory: true)
      do {
        documentUrl.path
        let files = try fileManager.contentsOfDirectoryAtPath(documentsDirectory)
        for file in files {
          fetchVideos(documentUrl.URLByAppendingPathComponent(file).path ?? "")
        }
      }  catch {
        
      }
      
      self.tableView.reloadData()
    }
  }
  
  func fetchVideos(path: String) {
    var isDir: ObjCBool = false
    if !path.isEmpty && fileManager.fileExistsAtPath(path, isDirectory: &isDir) {
      if isDir {
        do {
          let files = try fileManager.contentsOfDirectoryAtPath(path)
          for file in files {
            fetchVideos(file)
          }
        } catch {
        }
      } else {
        var file = File(path: path)
        if file.isValid() && isVideoFileExtension(file.fileExtension.uppercaseString) {
          do {
            if let attr: NSDictionary = try fileManager.attributesOfItemAtPath(path) {
              file.fileSize = attr.fileSize()
            }
          } catch {
          }
          videos.append(file)
        }
      }
    }
  }
  
  func isVideoFileExtension(ext: String) -> Bool {
    for videoExtension in VIDEO_EXTENSIONS {
      if ext == videoExtension {
        return true
      }
    }
    return false
  }
  
  struct File {
    var fileExtension = ""
    var fileName = ""
    var path = ""
    var assert: AVURLAsset?
    var url: NSURL!
    var fileSize: UInt64 = 0
    
    init(path: String) {
      self.path = path
      self.url = NSURL(fileURLWithPath: path)
      self.fileName = url.lastPathComponent ?? ""
      self.fileExtension = "." + (url.pathExtension ?? "")
    }
    
    func isValid() -> Bool {
      return !(fileName.isEmpty || fileExtension.isEmpty)
    }
  }

代碼說(shuō)明:

      a)需要注意一些 swift 的用法,例如 fileExistsAtPath 的用法

      b)還有 String 的 pathExtension 和 lastPathComponent 都沒(méi)了,都改到了 NSURL 下面去了,網(wǎng)上很多資料都還是從 NSString 或者 String 取這些屬性

      c)AVURLAsset 可以取到視頻的時(shí)長(zhǎng) CMTimeGetSeconds(AVURLAsset(URL: file.url, options: nil).duration)

  三、播放視頻

 func play(file: File) {
    let player = AVPlayer(URL: file.url)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.presentViewController(playerViewController, animated: true) {
      playerViewController.player?.play()
    }
  }

        四、用 ... 打開(kāi)

 func openIn(file: File, indexPath: NSIndexPath) {
    let document = UIDocumentInteractionController(URL: file.url)
    let rect = self.tableView.rectForRowAtIndexPath(indexPath)
    document.presentOpenInMenuFromRect(rect, inView: self.tableView, animated: true)
  }

        五、刪除視頻

 func delete(file: File, indexPath: NSIndexPath) {
    do {
      try fileManager.removeItemAtPath(file.path)
      videos.removeAtIndex(indexPath.row)
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    } catch {
      
    }
  }

        六、保存到相冊(cè)

 func saveToCameraRoll(file: File, indexPath: NSIndexPath) {
    if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(file.path) {
      UISaveVideoAtPathToSavedPhotosAlbum(file.path, self, "image:didFinishSavingWithError:contextInfo:", nil)
    } else {
      // save faild
    }
  }
  
  func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
    if error == nil {
      // save success
    } else {
      // save faild
    }
  }

 代碼說(shuō)明:

      注意 UISaveVideoAtPathToSavedPhotosAlbum 的用法,后面 Selector 寫(xiě)得不對(duì)就會(huì)報(bào)錯(cuò)。

以上就是IOS 8 共享文件的實(shí)例代碼,有需要的朋友可以參考下。

相關(guān)文章

最新評(píng)論

镇康县| 霍邱县| 襄汾县| 巴林右旗| 三明市| 宁海县| 普格县| 西城区| 金寨县| 陇西县| 平凉市| 新丰县| 南江县| 吉林省| 汶川县| 兰州市| 从江县| 鄯善县| 德化县| 南投县| 新宾| 吕梁市| 安达市| 同仁县| 松滋市| 株洲市| 墨江| 台东县| 兴义市| 南宫市| 长阳| 南华县| 南开区| 大英县| 桃园市| 元阳县| 陈巴尔虎旗| 望谟县| 勃利县| 乌什县| 诏安县|