1
votes

I've made a UIWebView to show a gif and i have 2 buttons login and register.

First of all when I run it my ram uses 270mb then if I click to a button goes to another viewcontroller (270mb stays) then if I go back it's like it's loading another gif and the ram goes up to 430mb.

I have already test the webview.stoploading() when button clicked

 override func viewDidLoad() {
    super.viewDidLoad()
let filePath = NSBundle.mainBundle().pathForResource("railway", ofType: "gif")
    let gif = NSData(contentsOfFile: filePath!)
    webview.loadData(gif!, MIMEType: "image/gif", textEncodingName: String(), baseURL: NSURL())
    webview.userInteractionEnabled = false; }



    @IBAction func login(sender: AnyObject) {

    webview.stopLoading()
}
2
UIWebView has memory leaks. Switching to WKWebView might solve your problems. For more info look: stackoverflow.com/questions/28401650/memory-leak-with-uiwebview and here also: nshipster.com/wkwebkitAndrej

2 Answers

3
votes

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")
}
0
votes

ok i found it out!!!! When i drag and drop the connection (from login button to the login view) i must set it to push (i set it with show) now i use 97mb ram, 130mb when i go the login view and 97mb when i go back.

First of all i used video now but i believe its the same thing as the problem was the same Second if someone want to try it he must embed in the controllers with nav controllers(if you want you can hide the bar to the first view) and you will drag from the button to the second view with push (not to its nav controller)