Try to load your contents by using cache policy:
enum NSURLRequestCachePolicy : UInt {
case UseProtocolCachePolicy
case ReloadIgnoringLocalCacheData
case ReloadIgnoringLocalAndRemoteCacheData
static var ReloadIgnoringCacheData: NSURLRequestCachePolicy { get }
case ReturnCacheDataElseLoad
case ReturnCacheDataDontLoad
case ReloadRevalidatingCacheData }
So your code can be:
let filePath = NSBundle.mainBundle().pathForResource("railway", ofType: "gif")
var requestURL = NSURL(string:filePath!);
var request = NSMutableURLRequest(URL: requestURL!,
cachePolicy: .ReturnCacheDataElseLoad,
timeoutInterval: 15.0)
webview.loadRequest(request)
You can also handle the NSURLCache memory :
override func viewDidLoad() {
super.viewDidLoad()
let cacheSizeMemory = 8*1024*1024; // 8MB
let cacheSizeDisk = 32*1024*1024; // 32MB
let sharedCache = NSURLCache.init(
memoryCapacity:cacheSizeMemory,
diskCapacity:cacheSizeDisk,
diskPath: "nsurlcache"
)
NSURLCache.setSharedURLCache(sharedCache)
}
override func didReceiveMemoryWarning() {
print("Received memory warning")
NSURLCache.sharedURLCache().removeAllCachedResponses()
super.didReceiveMemoryWarning()
}
Another thing you can do with your project is to cache your object with a code like this:
let cache = NSCache()
let myGiantObject: GiantObjectClass
if let cachedVersion = cache.objectForKey("GiantObjectClassCached") as? GiantObjectClass {
// use the cached version
myGiantObject = cachedVersion
} else {
// create it from the original constructors then store in the cache
myGiantObject = GiantObjectClass()
cache.setObject(myObject, forKey: "GiantObjectClassCached")
}
UIWebView
has memory leaks. Switching toWKWebView
might solve your problems. For more info look: stackoverflow.com/questions/28401650/memory-leak-with-uiwebview and here also: nshipster.com/wkwebkit – Andrej