jeudi 26 mars 2015

Storing an image from the web for one session (in Swift)

I am following a tutorial about getting images from the web and storing them on the phone in Swift. For my purpose, I would like to know how I could only store them for one 'session', which means until the user stops using the app. The reason is that I want to change the image of the url every day. Anyone any idea?



@IBOutlet var overLay: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.



let url = NSURL(string: "http://test.com")

// Update - changed url to url!

let urlRequest = NSURLRequest(URL: url!)

NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in

if error != nil {

println("There was an error")

} else {

let image = UIImage(data: data)

// self.overLay.image = image

var documentsDirectory:String?

var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

if paths.count > 0 {

documentsDirectory = paths[0] as? String

var savePath = documentsDirectory! + "/overLay.jpg"

NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)

self.overLay.image = UIImage(named: savePath)

}

}


})

}


thank you so much!




Aucun commentaire:

Enregistrer un commentaire