|
|
|
@ -12,12 +12,30 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
var items = [MediaItem]() |
|
|
|
var completionHandler: ((Void) -> Void)? |
|
|
|
|
|
|
|
var requests = Array<ImageLoadOperation>() |
|
|
|
|
|
|
|
// var fetchers = [NetworkFetcher<UIImage>]() |
|
|
|
|
|
|
|
let hqCache = Shared.imageCache // Cache<UIImage>(name: "hq") |
|
|
|
|
|
|
|
lazy var operationQueue: NSOperationQueue = { |
|
|
|
var queue = NSOperationQueue() |
|
|
|
queue.name = "Photo queue" |
|
|
|
queue.maxConcurrentOperationCount = 1 |
|
|
|
return queue |
|
|
|
}() |
|
|
|
|
|
|
|
override func viewDidLoad() { |
|
|
|
super.viewDidLoad() |
|
|
|
|
|
|
|
hqCache.addFormat(Format<UIImage>(name: "icons", diskCapacity: 0)) |
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
setChromeVisibility(true, animated: true) |
|
|
|
|
|
|
|
self.photoAlbumView.reloadData(); |
|
|
|
@ -28,6 +46,24 @@ class MediaPhotoController: NIToolbarPhotoViewController, NIPhotoAlbumScrollView |
|
|
|
completionHandler!() |
|
|
|
} |
|
|
|
|
|
|
|
func play() { |
|
|
|
let controller = VideoPlayerController() |
|
|
|
controller.edit = false |
|
|
|
controller.allowEdit = false |
|
|
|
|
|
|
|
let currentItem = items[photoAlbumView.centerPageIndex] |
|
|
|
controller.currentItem = currentItem |
|
|
|
controller.navigationItem.leftItemsSupplementBackButton = true |
|
|
|
navigationController!.navigationBar.barTintColor = UIColor.blackColor() |
|
|
|
navigationController!.pushViewController(controller, animated: true) |
|
|
|
|
|
|
|
controller.completionHandler = { |
|
|
|
() in |
|
|
|
// NetworkManager.sharedInstance.saveItem(self.currentItem!) |
|
|
|
self.dismissViewControllerAnimated(true, completion: nil); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override func loadView() { |
|
|
|
super.loadView() |
|
|
|
photoAlbumView.dataSource = self |
|
|
|
@ -58,35 +94,144 @@ println("\(count(items))") |
|
|
|
let c = count(items) |
|
|
|
println ("Index: \(photoAtIndex) of \(c)") |
|
|
|
let newItem = items[photoAtIndex] |
|
|
|
let URL = NSURL(string: newItem.imageUrlAbsolute)! |
|
|
|
println (newItem.imageUrlAbsolute) |
|
|
|
let hqURL = NSURL(string: newItem.imageUrlAbsolute)! |
|
|
|
var image: UIImage? = nil |
|
|
|
|
|
|
|
var size = NIPhotoScrollViewPhotoSizeUnknown |
|
|
|
|
|
|
|
Shared.imageCache.fetch(URL: URL).onSuccess { |
|
|
|
hqCache.fetch(key: hqURL.absoluteString!, formatName: HanekeGlobals.Cache.OriginalFormatName).onSuccess { |
|
|
|
i in |
|
|
|
println ("cached \(newItem.imageUrlAbsolute)") |
|
|
|
image = i |
|
|
|
size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
} |
|
|
|
// var fetcher = Shared.imageCache.fetch(URL: URL) |
|
|
|
// let fs = Shared.imageCache.formats |
|
|
|
// if let (format, memoryCache, diskCache) = fs[HanekeGlobals.Cache.OriginalFormatName] { |
|
|
|
// if memoryCache.objectForKey(URL!.absoluteString!) != nil { |
|
|
|
// Shared.imageCache.fetch(URL: URL).onSuccess { |
|
|
|
// i in |
|
|
|
// println ("loaded \(newItem.imageUrlAbsolute)") |
|
|
|
// image = i |
|
|
|
// size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
// self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
// if (fetcher != nil) { |
|
|
|
// fetcher!.cancelFetch() |
|
|
|
// } |
|
|
|
// fetcher = NetworkFetcher<UIImage>(URL: URL) |
|
|
|
// let fetch = Shared.imageCache.fetch(fetcher: fetcher!, formatName: HanekeGlobals.Cache.OriginalFormatName, failure: nil, success: nil) |
|
|
|
// |
|
|
|
// fetch.onSuccess { |
|
|
|
// i in |
|
|
|
// println ("loaded \(newItem.imageUrlAbsolute)") |
|
|
|
// image = i |
|
|
|
// size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
// self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
// } |
|
|
|
|
|
|
|
if let i = image { |
|
|
|
isLoading[0] = false |
|
|
|
} |
|
|
|
else { |
|
|
|
println ("not cached \(newItem.imageUrlAbsolute)") |
|
|
|
|
|
|
|
let URL = NSURL(string: newItem.thumbUrlAbsolute)! |
|
|
|
println (newItem.thumbUrlAbsolute) |
|
|
|
var image: UIImage? = nil |
|
|
|
|
|
|
|
Shared.imageCache.fetch(URL: URL).onSuccess { |
|
|
|
for r in requests { |
|
|
|
let pages = self.photoAlbumView.visiblePages() as NSMutableSet! |
|
|
|
var ok = false |
|
|
|
for page in pages.allObjects { |
|
|
|
if (page.pageIndex == r.index) { |
|
|
|
ok = true |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if !ok { |
|
|
|
println("canceled \(r.imageURL)") |
|
|
|
r.cancel() |
|
|
|
requests.remove(r) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// let op = HanekeFetchOperation(baseUrl: URL, succeeder: { |
|
|
|
// i in |
|
|
|
// image = i |
|
|
|
// size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
// self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
// println ("thumb \(URL.absoluteString!)") |
|
|
|
// photoSize[0] = size |
|
|
|
// }, index: photoAtIndex) |
|
|
|
// requests.append(op) |
|
|
|
// self.operationQueue.addOperation(op) |
|
|
|
|
|
|
|
let fetcher = NetworkFetcher<UIImage>(URL: URL) |
|
|
|
let fetch = Shared.imageCache.fetch(fetcher: fetcher, formatName: HanekeGlobals.Cache.OriginalFormatName, failure: nil, success: nil) |
|
|
|
|
|
|
|
fetch.onSuccess { |
|
|
|
i in |
|
|
|
println ("loaded \(newItem.imageUrlAbsolute)") |
|
|
|
image = i |
|
|
|
size = NIPhotoScrollViewPhotoSizeThumbnail |
|
|
|
self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let op2 = ImageLoadOperation(imageURL: hqURL, succeeder: { |
|
|
|
i in |
|
|
|
println ("loaded \(newItem.imageUrlAbsolute) at \(photoAtIndex)") |
|
|
|
image = i |
|
|
|
size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
photoSize[0] = size |
|
|
|
}, index: photoAtIndex) |
|
|
|
requests.append(op2) |
|
|
|
op2.qualityOfService = NSQualityOfService.UserInteractive |
|
|
|
self.operationQueue.addOperation(op2) |
|
|
|
|
|
|
|
|
|
|
|
// NSURLSession.sharedSession().invalidateAndCancel(); |
|
|
|
|
|
|
|
// for f in fetchers { |
|
|
|
// println ("cancel \(f.key)") |
|
|
|
// f.cancelFetch() |
|
|
|
// } |
|
|
|
// fetchers.removeAll() |
|
|
|
|
|
|
|
// let fetcher = NetworkFetcher<UIImage>(URL: hqURL) |
|
|
|
// fetchers.append(fetcher) |
|
|
|
// |
|
|
|
// let fetch = hqCache.fetch(fetcher: fetcher, formatName: "icons", failure: nil, success: nil) |
|
|
|
// |
|
|
|
// fetch.onSuccess { |
|
|
|
// i in |
|
|
|
// println ("loaded \(newItem.imageUrlAbsolute) at \(photoAtIndex)") |
|
|
|
// image = i |
|
|
|
// size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
// self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
// photoSize[0] = size |
|
|
|
// return |
|
|
|
// } |
|
|
|
|
|
|
|
// Shared.imageCache.fetch(URL: hqURL).onSuccess { |
|
|
|
// i in |
|
|
|
// println ("loaded \(newItem.imageUrlAbsolute)") |
|
|
|
// image = i |
|
|
|
// size = NIPhotoScrollViewPhotoSizeOriginal |
|
|
|
// self.photoAlbumView.didLoadPhoto(image, atIndex:photoAtIndex, photoSize:size) |
|
|
|
// } |
|
|
|
|
|
|
|
if (image == nil) { |
|
|
|
isLoading[0] = true |
|
|
|
} |
|
|
|
} |
|
|
|
photoSize[0] = size |
|
|
|
|
|
|
|
// if image == nil { return UIImage(named: "Kirschkeks-256x256.png") } |
|
|
|
@ -130,7 +275,7 @@ println (newItem.imageUrlAbsolute) |
|
|
|
} |
|
|
|
|
|
|
|
let c = count(items) |
|
|
|
println ("Index: \(thumbnailIndex) of \(c)") |
|
|
|
println ("Thumb Index: \(thumbnailIndex) of \(c)") |
|
|
|
let newItem = items[thumbnailIndex] |
|
|
|
let URL = NSURL(string: newItem.thumbUrlAbsolute)! |
|
|
|
|
|
|
|
@ -139,6 +284,8 @@ println (newItem.imageUrlAbsolute) |
|
|
|
Shared.imageCache.fetch(URL: URL).onSuccess { |
|
|
|
i in |
|
|
|
image = i |
|
|
|
println ("thumb scrubber \(URL.absoluteString!)") |
|
|
|
|
|
|
|
self.photoScrubberView.didLoadThumbnail(i, atIndex: thumbnailIndex); |
|
|
|
} |
|
|
|
// if image == nil { return UIImage(named: "Kirschkeks-256x256.png") } |
|
|
|
|