-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIImageView.swift
executable file
·69 lines (60 loc) · 2.59 KB
/
UIImageView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// UIImageView.swift
// School app
//
// Created by philip mackie on 11/3/15.
// Copyright © 2015 vilet studios. All rights reserved.
//
import UIKit
extension UIImageView {
func setRandomDownloadImage(width: Int, height: Int) {
if self.image != nil {
self.alpha = 1
return
}
self.alpha = 0
let url = NSURL(string: "http://en.laudeladyelizabeth.com/\(width)/\(height)/")!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 15
configuration.timeoutIntervalForResource = 15
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
let session = NSURLSession(configuration: configuration)
let task = session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
if error != nil {
return
}
if let response = response as? NSHTTPURLResponse {
if response.statusCode / 100 != 2 {
return
}
if let data = data, let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.image = image
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.alpha = 1
}) { (finished: Bool) -> Void in
}
})
}
}
})
task.resume()
}
func clipParallaxEffect(baseImage: UIImage?, screenSize: CGSize, displayHeight: CGFloat) {
if let baseImage = baseImage {
if displayHeight < 0 {
return
}
let aspect: CGFloat = screenSize.width / screenSize.height
let imageSize = baseImage.size
let imageScale: CGFloat = imageSize.height / screenSize.height
let cropWidth: CGFloat = floor(aspect < 1.0 ? imageSize.width * aspect : imageSize.width)
let cropHeight: CGFloat = floor(displayHeight * imageScale)
let left: CGFloat = (imageSize.width - cropWidth) / 2
let top: CGFloat = (imageSize.height - cropHeight) / 2
let trimRect : CGRect = CGRectMake(left, top, cropWidth, cropHeight)
self.image = baseImage.trim(trimRect: trimRect)
self.frame = CGRectMake(0, 0, screenSize.width, displayHeight)
}
}
}