How to read data files Application document catalog in swift?

I am new to Swift programming language. I want to download a movie from some tubular servers and want to play offline. I use Alamofire to download parts. I can specify the file as follows:

var file:String?
if let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(documentsDirectory, error: &error) as? [String] {
                for filename in files {
                    // do stuff with filename
                    file = filename
                    println(filename)
                }
            }

But the problem is how I can use this file for my purpose. Suppose its image file, and I want to show in imageview.

myImageView.image = UIImage(contentsOfFile: file) /* doesn't work*/

Thank you for any help.

+4
source share
3 answers

For Swift 2, you need to change something. Note. stringByAppendingPathComponent is no longer available for String (NSString only):

var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var getImagePath = paths.stringByAppendingPathComponent("filename")
myImageView.image = UIImage(contentsOfFile: getImagePath)
+6
source

Try this code:

var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var getImagePath = paths.stringByAppendingPathComponent("filename")
myImageView.image = UIImage(contentsOfFile: getImagePath)

, .

+6

UIImage (contentsOfFile). , ..:

myImageView.image = UIImage(contentsOfFile: "\(documentsDirectory!)/\(filename)")  //hopefully does work!
-3

All Articles