连续第二次在Apple的TestFlight评论中遭到拒绝。这次从他们那里得到了崩溃日志,显然我的应用程序看起来正在崩溃异步图像下载和缓存(在UITableView中使用)。几次检查代码,但仍然不知道什么是错的,应用程序在没有崩溃的设备和模拟器上工作,有和没有互联网连接。请看一下突出显示的崩溃线:
var imgCache = [String: UIImage]()
func downloadAndCacheImg(url: String?, completion: ((UIImage)->())?) {
if url == nil { return }
if let img = imgCache[url!] {
completion?(img)
return
}
if let img = UIImage(contentsOfFile: formatFileNameForCachedImg(url!)) {
self.imgCache[url!] = img
completion?(img)
return
}
if let nsurl = NSURL(string: url!) {
NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: nsurl), queue: NSOperationQueue.mainQueue(), completionHandler: { (response, data, error) -> Void in
if let img = UIImage(data: data) {
self.imgCache[url!] = img <<<<< CRASH HERE
data.writeToFile(self.formatFileNameForCachedImg(url!), atomically: true)
completion?(img)
}
})
}
}
func formatFileNameForCachedImg(url: String) -> String {
let dir = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
let dirName = dir[0] as! String
var fname = url.stringByReplacingOccurrencesOfString("/", withString: "", options: nil, range: nil)
let out = String(format: "%@/%@", dirName, fname)
return out
}