|
|
|
@ -15,61 +15,87 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
|
|
|
|
var requests = Array<ImageLoadOperation>() |
|
|
|
|
|
|
|
let THUMBS = "thumb" |
|
|
|
var imageCache = NSCache() |
|
|
|
var total = 0 |
|
|
|
|
|
|
|
var imageCache = Cache<UIImage>(name: "thumbnails") |
|
|
|
lazy var backgroundOperationQueue: NSOperationQueue = { |
|
|
|
var queue = NSOperationQueue() |
|
|
|
queue.name = "Thumb queue" |
|
|
|
queue.maxConcurrentOperationCount = 1 |
|
|
|
return queue |
|
|
|
}() |
|
|
|
|
|
|
|
lazy var operationQueue: NSOperationQueue = { |
|
|
|
var queue = NSOperationQueue() |
|
|
|
queue.name = "Photo queue" |
|
|
|
queue.maxConcurrentOperationCount = 5 |
|
|
|
queue.maxConcurrentOperationCount = 6 |
|
|
|
return queue |
|
|
|
}() |
|
|
|
|
|
|
|
override func viewDidLoad() { |
|
|
|
let thumbFormat = Format<UIImage>(name: THUMBS, diskCapacity: 100 * 1024 * 1024) |
|
|
|
imageCache.addFormat(thumbFormat) |
|
|
|
override func viewDidDisappear(animated: Bool) { |
|
|
|
backgroundOperationQueue.cancelAllOperations() |
|
|
|
} |
|
|
|
|
|
|
|
override func viewDidLoad() { |
|
|
|
super.viewDidLoad() |
|
|
|
|
|
|
|
imageCache.totalCostLimit = 1024 * 1024 * 50 |
|
|
|
|
|
|
|
let backButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("back")) |
|
|
|
navigationItem.leftBarButtonItems = [backButton] |
|
|
|
|
|
|
|
let playButton = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: Selector("play")) |
|
|
|
navigationItem.rightBarButtonItems = [playButton] |
|
|
|
let shotButton = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("shot")) |
|
|
|
navigationItem.rightBarButtonItems = [playButton, shotButton] |
|
|
|
|
|
|
|
setChromeVisibility(true, animated: true) |
|
|
|
|
|
|
|
self.photoAlbumView.reloadData(); |
|
|
|
self.photoScrubberView.reloadData(); |
|
|
|
|
|
|
|
let back = NSURLSessionConfiguration.ephemeralSessionConfiguration() |
|
|
|
|
|
|
|
let bsession = NSURLSession(configuration: back) |
|
|
|
|
|
|
|
var j = 0; |
|
|
|
|
|
|
|
for i in items { |
|
|
|
if j++ > 1000 { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
let URL = NSURL(string: i.thumbUrlAbsolute)! |
|
|
|
let fetcher = MediaFetcher<UIImage>(URL: URL, session: bsession) |
|
|
|
let fetch = imageCache.fetch(fetcher: fetcher, formatName: THUMBS, failure: nil, success: nil) |
|
|
|
|
|
|
|
fetch.onSuccess { |
|
|
|
s in |
|
|
|
println ("preload \(s)") |
|
|
|
} |
|
|
|
let op1 = ImageLoadOperation(imageURL: URL, succeeder: { |
|
|
|
img in |
|
|
|
let imageRef = img.CGImage; |
|
|
|
let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; |
|
|
|
let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; |
|
|
|
self.total += cost |
|
|
|
println("preload image loaded \(i.thumbUrlAbsolute) cost \(cost) total\(self.total)") |
|
|
|
self.imageCache.setObject(img, forKey: i.thumbUrlAbsolute, cost: cost) |
|
|
|
}, index: j) |
|
|
|
op1.qualityOfService = NSQualityOfService.Background |
|
|
|
backgroundOperationQueue.addOperation(op1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override func didReceiveMemoryWarning() { |
|
|
|
println("warning") |
|
|
|
imageCache.removeAllObjects() |
|
|
|
|
|
|
|
super.didReceiveMemoryWarning() |
|
|
|
// Dispose of any resources that can be recreated. |
|
|
|
} |
|
|
|
|
|
|
|
func shot() { |
|
|
|
let currentItem = items[photoAlbumView.centerPageIndex] |
|
|
|
var imageUrl = currentItem.thumbUrl!.stringByReplacingOccurrencesOfString("_thumb.jpg", withString: ".jpg") |
|
|
|
imageUrl = imageUrl.stringByReplacingOccurrencesOfString("?preview=true", withString: "") |
|
|
|
let url = NetworkManager.sharedInstance.baseurl + "/service/linkfavpic" + imageUrl |
|
|
|
|
|
|
|
println(url) |
|
|
|
Alamofire.request(.GET, url).responseString { |
|
|
|
(_, _, string, _) in |
|
|
|
println("ok") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func back() { |
|
|
|
completionHandler!() |
|
|
|
@ -194,27 +220,6 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
|
|
|
|
var size = NIPhotoScrollViewPhotoSizeUnknown |
|
|
|
|
|
|
|
imageCache.fetch(key: URL.absoluteString!, formatName: THUMBS).onSuccess { |
|
|
|
i in |
|
|
|
image = i |
|
|
|
size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
self.photoAlbumView.didLoadPhoto(image, atIndex: photoAtIndex, photoSize: size) |
|
|
|
} |
|
|
|
|
|
|
|
if let i = image { |
|
|
|
isLoading[0] = false |
|
|
|
} else { |
|
|
|
let fetcher = NetworkFetcher<UIImage>(URL: URL) |
|
|
|
let fetch = imageCache.fetch(fetcher: fetcher, formatName: THUMBS, failure: nil, success: nil) |
|
|
|
|
|
|
|
fetch.onSuccess { |
|
|
|
i in |
|
|
|
println("thumb loaded \(newItem.imageUrlAbsolute)") |
|
|
|
size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
self.photoAlbumView.didLoadPhoto(image, atIndex: photoAtIndex, photoSize: size) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for r in requests { |
|
|
|
let pages = self.photoAlbumView.visiblePages() as NSMutableSet! |
|
|
|
var ok = false |
|
|
|
@ -231,14 +236,39 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
image = imageCache.objectForKey(URL.absoluteString!) as? UIImage |
|
|
|
|
|
|
|
if let i = image { |
|
|
|
size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
isLoading[0] = false |
|
|
|
} else { |
|
|
|
let URL = NSURL(string: newItem.thumbUrlAbsolute)! |
|
|
|
|
|
|
|
let op3 = ImageLoadOperation(imageURL: URL, succeeder: { |
|
|
|
i in |
|
|
|
// println("thumb preload image loaded \(newItem.imageUrlAbsolute)") |
|
|
|
let imageRef = i.CGImage; |
|
|
|
let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; |
|
|
|
let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; |
|
|
|
self.imageCache.setObject(i, forKey: newItem.thumbUrlAbsolute, cost: cost) |
|
|
|
size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
self.photoAlbumView.didLoadPhoto(i, atIndex: photoAtIndex, photoSize: size) |
|
|
|
self.photoScrubberView.didLoadThumbnail(i, atIndex: photoAtIndex); |
|
|
|
}, index: photoAtIndex) |
|
|
|
op3.qualityOfService = NSQualityOfService.UserInteractive |
|
|
|
requests.append(op3) |
|
|
|
operationQueue.addOperation(op3) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let op2 = ImageLoadOperation(imageURL: hqURL, succeeder: { |
|
|
|
i in |
|
|
|
println("image loaded \(newItem.imageUrlAbsolute) at \(photoAtIndex)") |
|
|
|
// println("image loaded \(newItem.imageUrlAbsolute) at \(photoAtIndex)") |
|
|
|
size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
self.photoAlbumView.didLoadPhoto(i, atIndex: photoAtIndex, photoSize: size) |
|
|
|
photoSize[0] = size |
|
|
|
}, index: photoAtIndex) |
|
|
|
op2.qualityOfService = NSQualityOfService.UserInitiated |
|
|
|
requests.append(op2) |
|
|
|
operationQueue.addOperation(op2) |
|
|
|
|
|
|
|
@ -288,18 +318,29 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
|
|
|
|
let c = count(items) |
|
|
|
let newItem = items[thumbnailIndex] |
|
|
|
let URL = NSURL(string: newItem.thumbUrlAbsolute)! |
|
|
|
|
|
|
|
let fetcher = NetworkFetcher<UIImage>(URL: URL) |
|
|
|
let fetch = imageCache.fetch(fetcher: fetcher, formatName: THUMBS, failure: nil, success: nil) |
|
|
|
var image: UIImage? |
|
|
|
|
|
|
|
image = imageCache.objectForKey(newItem.thumbUrlAbsolute) as? UIImage |
|
|
|
|
|
|
|
fetch.onSuccess { |
|
|
|
if (image == nil) { |
|
|
|
let URL = NSURL(string: newItem.thumbUrlAbsolute)! |
|
|
|
let op3 = ImageLoadOperation(imageURL: URL, succeeder: { |
|
|
|
i in |
|
|
|
// println("thumb image loaded \(newItem.thumbUrlAbsolute)") |
|
|
|
let imageRef = i.CGImage; |
|
|
|
let bytesPerPixel = CGImageGetBitsPerPixel(imageRef) / 8; |
|
|
|
let cost = CGImageGetWidth(imageRef) * CGImageGetHeight(imageRef) * bytesPerPixel; |
|
|
|
|
|
|
|
self.imageCache.setObject(i, forKey: newItem.thumbUrlAbsolute, cost: cost) |
|
|
|
|
|
|
|
self.photoScrubberView.didLoadThumbnail(i, atIndex: thumbnailIndex); |
|
|
|
}, index: thumbnailIndex) |
|
|
|
op3.qualityOfService = NSQualityOfService.UserInitiated |
|
|
|
operationQueue.addOperation(op3) |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
return image |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -311,5 +352,7 @@ class MediaFetcher<T : DataConvertible> : NetworkFetcher<T> { |
|
|
|
super.init(URL: URL) |
|
|
|
} |
|
|
|
|
|
|
|
override public var session : NSURLSession { return bsession! } |
|
|
|
override public var session: NSURLSession { |
|
|
|
return bsession! |
|
|
|
} |
|
|
|
} |