Created
December 30, 2016 09:06
-
-
Save ricardopereira/288747eadf4e45066c3beb64c6f22119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var image: UIImage! | |
var imageView: UIImageView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let avatarData = NSData(contentsOfURL: NSURL(string: "https://www.gravatar.com/avatar/7d7c8bd1f797a0e9b474e52c3968cbde?s=48&d=identicon&r=PG")!) | |
let landscapeData = NSData(contentsOfURL: NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/e/e4/Stourhead_garden.jpg")!) | |
let portraitData = NSData(contentsOfURL: NSURL(string: "http://1.bp.blogspot.com/-hNC-oT6f-fY/TeXxO26yjvI/AAAAAAAAAOY/qfkOqdKkBi8/s1600/platon-photographer-putin-man-of-the-year-portrait.jpg")!) | |
image = UIImage(data: landscapeData!) | |
imageView = UIImageView(image: image!) | |
view.backgroundColor = .whiteColor() | |
view.addSubview(imageView) | |
if image.size.height > image.size.width { | |
// Portrait | |
print("Portrait") | |
} | |
else { | |
// Landscape | |
print("Landscape") | |
} | |
print("mainScreen().bounds.width", UIScreen.mainScreen().bounds.width) | |
let finalHeight = UIScreen.mainScreen().bounds.width * image.size.height / image.size.width | |
print("finalHeight", finalHeight) | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activateConstraints([ | |
imageView.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 5), | |
imageView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor, constant: 5), | |
imageView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -5), | |
imageView.heightAnchor.constraintEqualToConstant(finalHeight), | |
]) | |
imageView.backgroundColor = .blueColor() | |
imageView.contentMode = .ScaleAspectFit | |
imageView.clipsToBounds = true | |
print("imageView.frame", imageView.frame) | |
print("image.size", image.size) | |
// --------- | |
let currentRunloop = CFRunLoopGetCurrent() | |
print("Before delay") | |
delay(3.0) { | |
print("Delay execution") | |
CFRunLoopPerformBlock(currentRunloop, NSRunLoopCommonModes) { | |
print("Perform block") | |
} | |
CFRunLoopWakeUp(currentRunloop) | |
} | |
print("After delay") | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
print("imageView.frame", imageView.frame) | |
print("image.size", image.size) | |
} | |
} | |
public func delay(seconds: NSTimeInterval, closure: ()->()) { | |
dispatch_after( | |
dispatch_time( | |
DISPATCH_TIME_NOW, | |
Int64(seconds * Double(NSEC_PER_SEC)) | |
), | |
dispatch_get_main_queue(), | |
closure | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment