Swift 下载文件,并读取
发布时间:2020-12-14 02:20:16 所属栏目:百科 来源:网络整理
导读:swift 下载图片并读取显示。 ···· override func viewDidLoad() { super.viewDidLoad() var request = HTTPTask() let downloadTask = request.download('http://www.test.com/pages_icon_large.png',parameters: nil,progress: {(complete: Double) in
···· override func viewDidLoad() { super.viewDidLoad() var request = HTTPTask() let downloadTask = request.download('http://www.test.com/pages_icon_large.png',parameters: nil,progress: {(complete: Double) in println(complete) },success: {(response: HTTPResponse) in self.downFile(response) },failure: {(error: NSError,response: HTTPResponse?) in println("failure") }) var fileManager = NSFileManager.defaultManager() var bundleURL = NSBundle.mainBundle().bundleURL var contents: NSArray = fileManager.contentsOfDirectoryAtURL(bundleURL,includingPropertiesForKeys: nil,options: .SkipsPackageDescendants,error: nil)! for URL in contents { //println(URL) } if let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true).first as? String { var enumerator = fileManager.enumeratorAtPath(path) println("enumrator: (path) (enumerator)") while let file: AnyObject = enumerator?.nextObject() { println(file) if let newPath = NSURL(fileURLWithPath: "(path)/(file)") { var image = UIImageView(image: UIImage(contentsOfFile: newPath.path!)) self.view.addSubview(image) } } } } func downFile(response: HTTPResponse) { if response.responSEObject != nil { //we MUST copy the file from its temp location to a permanent location. if let url = response.responSEObject as? NSURL { if let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,true).first as? String { if let fileName = response.suggestedFilename { if let newPath = NSURL(fileURLWithPath: "(path)/(fileName)") { let fileManager = NSFileManager.defaultManager() println(fileManager.fileExistsAtPath(newPath.path!)) if ( fileManager.fileExistsAtPath(newPath.path!) ) { println(newPath) dispatch_async(dispatch_get_main_queue()) { var image = UIImageView(image: UIImage(contentsOfFile: newPath.path!)) self.view.addSubview(image) println(image.frame) } }else{ fileManager.removeItemAtURL(newPath,error: nil) fileManager.moveItemAtURL(url,toURL: newPath,error: nil) println("创建文件") } } } } } } } ··· (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |