Skip to content

Commit

Permalink
Added LazyImageView object.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Example/LazyImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0830;
LastUpgradeCheck = 1010;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACE41AFB9204008FA782 = {
Expand Down Expand Up @@ -228,12 +228,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -281,12 +283,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LazyImage.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'LazyImage'
s.version = '6.6.1'
s.version = '6.7.0'
s.summary = 'Simple and efficient image lazy loading for iOS written in Swift'

# This description is used to generate tags and improve search results.
Expand Down
75 changes: 75 additions & 0 deletions Library/LazyImageView.swift
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)
}
}
}
}
}
}
}
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
Simple and efficient image lazy loading functionality for the iOS written in Swift.
LazyImage offers ease of use and complete control over your images.

Version 6.6.1


### Features
* Asynchronous image downloader on a background thread. Main thread is never blocked.
Expand Down Expand Up @@ -38,9 +36,28 @@ pod 'LazyImage'

### Usage

### LazyImageView

The simplest way to show an image on an image view is by setting the imageURL property

Example:
```swift
@IBOutlet weak var imageView: LazyImageView!

//Normal image
imageview.image = UIImage(named:"someAsset")

//Network image
imageView.imageURL = "https://domain.com/thepathtotheimage.png"
```

### LazyImage

#### For more options you can use the LazyImage object.

#### Show an image on an imageView

Create an image object that will hold the instance
Create a lazy image object that will hold the instance.

It is best that you create one instance per object responsible for the image
```swift
Expand Down Expand Up @@ -153,6 +170,7 @@ self.lazyImage.fetchImage(url: url) {
}
```

### Extra options

#### Zoom the image
```swift
Expand Down

0 comments on commit 8643fef

Please sign in to comment.