You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.8 KiB
73 lines
1.8 KiB
//
|
|
// Created by Marco Schmickler on 21.03.15.
|
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
import Darwin
|
|
|
|
public class ImageLoadOperation: Operation {
|
|
public typealias Succeeder = (UIImage) -> ()
|
|
|
|
let imageURL: URL
|
|
let succeeder: Succeeder
|
|
let index: Int
|
|
var request: DataRequest?
|
|
|
|
var manager: Session?
|
|
|
|
public init(imageURL: URL, succeeder: @escaping Succeeder, index: Int) {
|
|
self.imageURL = imageURL
|
|
self.succeeder = succeeder
|
|
self.index = index
|
|
}
|
|
|
|
override public func main() {
|
|
if imageURL.isFileURL {
|
|
usleep(100000)
|
|
}
|
|
|
|
if self.isCancelled {
|
|
return
|
|
}
|
|
|
|
print("lazy fetch image \(imageURL)")
|
|
if let data = try? Data(contentsOf: imageURL) {
|
|
// print("lazy fetch image ready \(imageURL)")
|
|
if self.isCancelled {
|
|
return
|
|
}
|
|
if let img = UIImage(data: data, scale: 1) {
|
|
if imageURL.isFileURL {
|
|
DispatchQueue.main.async {
|
|
self.succeeder(img)
|
|
}
|
|
return
|
|
}
|
|
if let decodedImg = img.decodeImage() {
|
|
if self.isCancelled {
|
|
// print("Decoding 2 cancelled")
|
|
return
|
|
}
|
|
DispatchQueue.main.async {
|
|
print("lazy fetch image decoded")
|
|
self.succeeder(decodedImg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override public func cancel() {
|
|
// if !self.isCancelled {
|
|
// print("cancel load \(imageURL)")
|
|
// }
|
|
if let r = request {
|
|
r.cancel()
|
|
}
|
|
|
|
super.cancel()
|
|
}
|
|
|
|
}
|