媒介
實用於 IOS 8 + 當地同享文件列表
注釋
1、預備
1.1 默許 App 的文件同享是封閉的,須要在 plist 中設置啟用:
Application supports iTunes file sharing 設置為 YES
啟用後把裝備銜接到 iTunes 上,在 iTunes 運用裡的文件同享就可以看到你的 App 了(假如看不見須要斷開從新拔插一下數據線),可以拷貝一些視頻出來,便於測試。
1.2 導入庫
Photos.framework
AVKit.framework 用於播放視頻
2、獲得視頻列表
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) } }
代碼解釋:
a)須要留意一些 swift 的用法,例如 fileExistsAtPath 的用法
b)還有 String 的 pathExtension 和 lastPathComponent 都沒了,都改到了 NSURL 上面去了,網上許多材料都照樣從 NSString 或許 String 取這些屬性
c)AVURLAsset 可以取到視頻的時長 CMTimeGetSeconds(AVURLAsset(URL: file.url, options: nil).duration)
3、播放視頻
func play(file: File) { let player = AVPlayer(URL: file.url) let playerViewController = AVPlayerViewController() playerViewController.player = player self.presentViewController(playerViewController, animated: true) { playerViewController.player?.play() } }
4、用 ... 翻開
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) }
5、刪除視頻
func delete(file: File, indexPath: NSIndexPath) { do { try fileManager.removeItemAtPath(file.path) videos.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } catch { } }
6、保留到相冊
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 } }
代碼解釋:
留意 UISaveVideoAtPathToSavedPhotosAlbum 的用法,前面 Selector 寫得纰謬就會報錯。
以上就是IOS 8 同享文件的實例代碼,有須要的同伙可以參考下。
【Swift 同享文件操作小結(iOS 8 +)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!