Verified Commit 2890572a authored by Geoff Pado's avatar Geoff Pado

Load and display image from photo asset

parent ee9e2026
......@@ -16,9 +16,29 @@ class PhotoEditingViewController: UIViewController {
view = PhotoEditingView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let options = PHImageRequestOptions()
options.version = .current
options.deliveryMode = .highQualityFormat
options.isNetworkAccessAllowed = true
imageManager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: options) { [weak self] image, info in
let isDegraded = (info?[PHImageResultIsDegradedKey] as? NSNumber)?.boolValue ?? false
guard let image = image, isDegraded == false else { return }
DispatchQueue.main.async { [weak self] in
self?.photoEditingView?.image = image
}
}
}
// MARK: Boilerplate
private let asset: PHAsset
private let imageManager = PHImageManager()
private var photoEditingView: PhotoEditingView? { return view as? PhotoEditingView }
@available(*, unavailable)
required init(coder: NSCoder) {
......@@ -29,12 +49,46 @@ class PhotoEditingViewController: UIViewController {
class PhotoEditingView: UIView {
init() {
imageView = PhotoEditingImageView()
super.init(frame: .zero)
backgroundColor = .primary
addSubview(imageView)
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: centerYAnchor),
imageView.widthAnchor.constraint(equalTo: widthAnchor),
imageView.heightAnchor.constraint(equalTo: heightAnchor)
])
}
var image: UIImage? {
get { return imageView.image }
set(newImage) {
imageView.image = newImage
}
}
// MARK: Boilerplate
private var imageView: PhotoEditingImageView
@available(*, unavailable)
required init(coder: NSCoder) {
let className = String(describing: type(of: self))
fatalError("\(className) does not implement init(coder:)")
}
}
class PhotoEditingImageView: UIImageView {
init() {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
contentMode = .scaleAspectFit
}
@available(*, unavailable)
required init(coder: NSCoder) {
let className = String(describing: type(of: self))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment