1

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()
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
mike vorisis
  • 2,786
  • 6
  • 40
  • 74
  • `UIWebView` has memory leaks. Switching to `WKWebView` might solve your problems. For more info look: http://stackoverflow.com/questions/28401650/memory-leak-with-uiwebview and here also: http://nshipster.com/wkwebkit/ – Andrej May 17 '16 at 07:53

2 Answers2

3

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")
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • it's better now but again if i move to another viewcontroller and back it gets double ram now the first launch take 170mb and after moving to viewcontrollers it goes up to 320mb – mike vorisis May 17 '16 at 08:14
  • and it corrected me another problem that i had when i was getting back from the second viewcontroller it took some time to load the gif again but not anymore. :) – mike vorisis May 17 '16 at 08:17
  • Good, what about "getting back from the second viewcontroller" can you post some code about it? segue? poptoviewcontroller? dismiss..? – Alessandro Ornano May 17 '16 at 08:19
  • All connections are from storyboard First view 1 webview, 2 buttons the login button connects with another viewcontroller just by drag and drop to the view And for getting back i have a navigation bar with bar button witch getting back to the first view – mike vorisis May 17 '16 at 08:33
  • nothing i tested all but nothing changed (maybe worse :P ) – mike vorisis May 17 '16 at 08:46
  • Sir, you are the man of this question, you must post it under question , not like an answer or many people can required to moderate your question.. – Alessandro Ornano May 17 '16 at 10:06
  • So you must convert your question in question-answer , flag to the left bottom of the form – Alessandro Ornano May 17 '16 at 10:11
0

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)

mike vorisis
  • 2,786
  • 6
  • 40
  • 74