-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can now fetch an image by setting the imageURL prooperty to LazyImageView.
- Loading branch information
Lampros Giampouras
committed
Dec 1, 2018
1 parent
3172d23
commit 8643fef
Showing
6 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
Example/LazyImage.xcodeproj/xcshareddata/xcschemes/LazyImage-Example.xcscheme
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// LazyImageView.swift | ||
// LazyImage | ||
// | ||
// Created by Lampros Giampouras on 01/12/2018. | ||
// | ||
|
||
import UIKit | ||
|
||
@objc protocol LazyImageViewDelegate: class { | ||
|
||
//Error downloading image | ||
@objc optional func errorDownloadingImage(url:String) -> Void | ||
} | ||
|
||
class LazyImageView: UIImageView { | ||
|
||
/// The delegate | ||
weak var delegate: LazyImageViewDelegate? | ||
|
||
/// The image url | ||
var imageURL:String? { | ||
didSet { | ||
self.loaded = false | ||
self.setNeedsLayout() | ||
self.layoutIfNeeded() | ||
} | ||
} | ||
|
||
/// Wether the image will be forced downloaded | ||
var forceDownload:Bool = false | ||
|
||
///The LazyImage object | ||
lazy var lazyImage:LazyImage = LazyImage() | ||
|
||
private var loaded:Bool = false | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
if (self.frame.size != CGSize.zero) && !self.loaded { | ||
self.loaded = true | ||
|
||
if let imageURL = self.imageURL { | ||
|
||
//Reset | ||
self.image = UIImage() | ||
|
||
let newSize = CGSize(width: self.frame.size.width, height: self.frame.size.height) | ||
|
||
if self.forceDownload { | ||
|
||
self.lazyImage.showOverrideWithSpinner(imageView:self, url:imageURL, size:newSize) { | ||
|
||
[weak self] (error:LazyImageError?) in | ||
|
||
if let _ = error { | ||
self?.delegate?.errorDownloadingImage?(url: imageURL) | ||
} | ||
} | ||
} | ||
else { | ||
self.lazyImage.showWithSpinner(imageView:self, url:imageURL, size:newSize) { | ||
|
||
[weak self] (error:LazyImageError?) in | ||
|
||
if let _ = error { | ||
self?.delegate?.errorDownloadingImage?(url: imageURL) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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