我做了一个UIWebView
来显示一个gif,我有2个按钮登录并注册。
首先,当我运行它时,我的ram使用 270mb 然后如果我点击一个按钮进入另一个viewcontroller(270mb停留)然后如果我回去它就像它正在加载另一个gif和ram上升到 430mb 。
我已经在点击按钮时测试了webview.stoploading()
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()
}
答案 0 :(得分:3)
尝试使用cache policy:
加载您的内容enum NSURLRequestCachePolicy : UInt { case UseProtocolCachePolicy case ReloadIgnoringLocalCacheData case ReloadIgnoringLocalAndRemoteCacheData static var ReloadIgnoringCacheData: NSURLRequestCachePolicy { get } case ReturnCacheDataElseLoad case ReturnCacheDataDontLoad case ReloadRevalidatingCacheData }
所以你的代码可以是:
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)
您还可以处理NSURLCache内存:
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()
}
您可以对项目执行的另一项操作是使用以下代码缓存对象:
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")
}
答案 1 :(得分:1)
首先我现在使用视频,但我相信它的问题是一样的 第二,如果有人想要尝试它,他必须使用导航控制器嵌入控制器(如果你想要你可以将栏隐藏到第一个视图),你将用按钮拖动按钮到第二个视图(而不是它的导航控制器)