From 2b0dd1833793a9f0dcb7ab95d740a472229f060e Mon Sep 17 00:00:00 2001 From: Marco Schmickler Date: Tue, 29 Dec 2015 18:27:29 +0100 Subject: [PATCH] Prefetch Bulk --- .idea/kplayer.iml | 80 +++++++------- kplayer/photo/MediaPhotoController.swift | 134 ++++++++++++++++++----- kplayer/util/DataLoadOperation.swift | 41 ++++++- 3 files changed, 186 insertions(+), 69 deletions(-) diff --git a/.idea/kplayer.iml b/.idea/kplayer.iml index 8455700..e2f5f66 100644 --- a/.idea/kplayer.iml +++ b/.idea/kplayer.iml @@ -5,62 +5,62 @@ - - - - + + - - - - + + + - + + + + - - - + + + + - - - + + - - + + - + @@ -96,8 +96,8 @@ - + @@ -107,7 +107,7 @@ - + @@ -115,30 +115,29 @@ + - - + + - + - + - - - - - + + + + - - + @@ -146,22 +145,23 @@ + - - + + + - - - - + + + - + @@ -182,12 +182,12 @@ - + + - diff --git a/kplayer/photo/MediaPhotoController.swift b/kplayer/photo/MediaPhotoController.swift index 691ac80..0c010ef 100644 --- a/kplayer/photo/MediaPhotoController.swift +++ b/kplayer/photo/MediaPhotoController.swift @@ -21,10 +21,12 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView var timer: NSTimer? + var currentIndex = 80 + lazy var backgroundOperationQueue: NSOperationQueue = { var queue = NSOperationQueue() queue.name = "Thumb queue" - queue.maxConcurrentOperationCount = 1 + queue.maxConcurrentOperationCount = 10 return queue }() @@ -42,7 +44,7 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView override func viewDidLoad() { super.viewDidLoad() - imageCache.totalCostLimit = 1024 * 1024 * 50 + imageCache.totalCostLimit = 1024 * 1024 * 1024 let backButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("back")) let slideButton = UIBarButtonItem(barButtonSystemItem: .FastForward, target: self, action: Selector("slideShow")) @@ -57,36 +59,118 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView self.photoAlbumView.reloadData(); self.photoScrubberView.reloadData(); - var j = 0; + preload(0, count: 40) + preload(40, count: 40) + } - for i in items { - if j++ > 1000 { - break - } + func preload(start: Int, count: Int) { + let dateFormatter = NSDateFormatter() + dateFormatter.dateFormat = "HH:mm:ss.SSSZ" - let u1 = NSURL(string: i.thumbUrlAbsolute) + var preview = "" + let end = start + count + for var k = start; k < end; k++ { + let tu = items[k].thumbUrl!.stringByReplacingOccurrencesOfString("?preview=true", withString: "") + preview += "\(tu);" + } - if let URL = u1 { + preview = preview.stringByAddingPercentEscapesUsingEncoding(NSISOLatin1StringEncoding)! + + let u2 = "\(NetworkManager.sharedInstance.baseurl)/service/preload?preview=\(preview)" + let u3 = NSURL(string: u2) + + if let URL = u3 { + let time = dateFormatter.stringFromDate(NSDate()) + print("\(time) preload image \(u2)") + + let op1 = DataLoadOperation(imageURL: URL, succeeder: { + da in + if da.length > 0 { + let d = da as! NSData + + let bytes = Array(UnsafeBufferPointer(start: UnsafePointer(d.bytes), count: d.length)) + let string1 = NSString(data: d, encoding: NSUTF8StringEncoding) + print(string1) + var index = 0 + let fHi = (Int(bytes[index++]) << 24)+(Int(bytes[index++]) << 16) + let f = fHi + (Int(bytes[index++]) << 8) + Int(bytes[index++]) +// let f = (bytes[index++] << 24)+(bytes[index++] << 16)+(bytes[index++] << 8) + bytes[index++] + let time = dateFormatter.stringFromDate(NSDate()) + print("\(time) start \(start) count \(f) size \(d.length)") + + var rest = self.items.count - self.currentIndex + if rest > 40 { + rest = 40 + } - let op1 = DataLoadOperation(imageURL: URL, succeeder: { - d in - if d.length > 0 { - if let img = UIImage(data: d) { - let imageRef = img.CGImage; - let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; - let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; - self.total += d.length - print("preload image loaded \(i.thumbUrlAbsolute) cost \(cost) total\(self.total)") - self.imageCache.setObject(img, forKey: i.thumbUrlAbsolute, cost: d.length) + if rest > 0 { + self.preload(self.currentIndex, count: rest) + self.currentIndex += rest + } + + for var bild = start; bild < end; bild++ { + let sizeHi = (Int(bytes[index++]) << 24)+(Int(bytes[index++]) << 16) + let size = (Int(bytes[index++]) << 8) + Int(bytes[index++]) + sizeHi + + if size > 0 { + // var buf = //UnsafeMutablePointer(&bytes[index]) + let part = d.subdataWithRange(NSMakeRange(index, size)) + if let img = UIImage(data: part) { + let imageRef = img.CGImage; + let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; + let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; + self.total += d.length + + let i = self.items[bild] +// print("\(time) preload combi image loaded \(i.name) cost \(cost) total\(self.total)") + self.imageCache.setObject(img, forKey: i.thumbUrlAbsolute, cost: size) + } } + index += size } - }, index: j) - op1.qualityOfService = NSQualityOfService.Background - backgroundOperationQueue.addOperation(op1) - } else { - print("## Invalid URL: \(i.thumbUrlAbsolute)") - } + + } + }, index: start) + op1.qualityOfService = NSQualityOfService.Background + backgroundOperationQueue.addOperation(op1) + } else { + print("## Invalid URL: \(u2)") } + + + +// for i in items { +// if j++ > 1000 { +// break +// } +// +// let u1 = NSURL(string: i.thumbUrlAbsolute) +// +// if let URL = u1 { +// let time = dateFormatter.stringFromDate(NSDate()) +// print("\(time) preload image \(i.name)") +// +// let op1 = DataLoadOperation(imageURL: URL, succeeder: { +// d in +// if d.length > 0 { +// if let img = UIImage(data: d) { +// let imageRef = img.CGImage; +// let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; +// let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; +// self.total += d.length +// +// let time = dateFormatter.stringFromDate(NSDate()) +// print("\(time) preload image loaded \(i.name) cost \(cost) total\(self.total)") +// self.imageCache.setObject(img, forKey: i.thumbUrlAbsolute, cost: d.length) +// } +// } +// }, index: j) +// op1.qualityOfService = NSQualityOfService.Background +// backgroundOperationQueue.addOperation(op1) +// } else { +// print("## Invalid URL: \(i.thumbUrlAbsolute)") +// } +// } } override func didReceiveMemoryWarning() { diff --git a/kplayer/util/DataLoadOperation.swift b/kplayer/util/DataLoadOperation.swift index 433edd6..8c3008a 100644 --- a/kplayer/util/DataLoadOperation.swift +++ b/kplayer/util/DataLoadOperation.swift @@ -7,6 +7,7 @@ import Foundation import Alamofire import Darwin + class DataLoadOperation: NSOperation { internal typealias Succeeder = (NSData) -> () @@ -27,11 +28,43 @@ class DataLoadOperation: NSOperation { if self.cancelled { return } + let dateFormatter = NSDateFormatter() + dateFormatter.dateFormat = "HH:mm:ss.SSS" + + var configuration = NSURLSessionConfiguration.defaultSessionConfiguration(); // backgroundSessionConfigurationWithIdentifier("imageLoad"); + configuration.HTTPMaximumConnectionsPerHost = 100; + var backgroundSession = NSURLSession(configuration: configuration, delegate: nil, delegateQueue: nil); + var url = NSURLRequest(URL: imageURL); + var downloadTask = backgroundSession.dataTaskWithRequest(url, completionHandler: { + data,response,error in + + if error == nil { + if let d = data { + // let time = dateFormatter.stringFromDate(NSDate()) + // print("\(time) preloaded image \(self.imageURL)") + + dispatch_async(dispatch_get_main_queue(), { + () -> Void in + self.succeeder(d) + }) + + } + } + else { + print(error!) + } + }) + + let time = dateFormatter.stringFromDate(NSDate()) +// print("\(time) preload image \(imageURL)") + + downloadTask.resume(); + - Alamofire.request(.GET, imageURL).response { - (_, _, d: AnyObject?, _) in - self.succeeder(d as! NSData) - }; +// Alamofire.request(.GET, imageURL).response { +// (_, _, d: AnyObject?, _) in +// self.succeeder(d as! NSData) +// }; } override func cancel() {