Skip to content

Commit

Permalink
Code optimisation
Browse files Browse the repository at this point in the history
Resizing image also happens in the background thread.
Code formatting update.
  • Loading branch information
Lampros Giampouras committed Nov 23, 2018
1 parent 1974423 commit 3172d23
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -70,7 +69,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
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.0'
s.version = '6.6.1'
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
74 changes: 17 additions & 57 deletions Library/LazyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// https://github.com/lamprosg/LazyImage

// Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
// Version 6.5.2
// Version 6.6.1


import Foundation
Expand Down Expand Up @@ -40,9 +40,6 @@ extension LazyImageError: LocalizedError {
}
}




public class LazyImage: NSObject {

var backgroundView:UIView?
Expand All @@ -53,7 +50,6 @@ public class LazyImage: NSObject {
var spinner:UIActivityIndicatorView? // Actual spinner
var desiredImageSize:CGSize?


//MARK: - URL string stripping

//TODO: Change this to hash the url
Expand All @@ -77,29 +73,34 @@ public class LazyImage: NSObject {

//MARK: - Image storage

/// The storage path for a given image unique name
///
/// - Parameter name: The unique name of the image
/// - Returns: Returns the storage path
private func storagePathforImageName(name:String) -> String {
return String(format:"%@/%@", NSTemporaryDirectory(), name)
}


/// Saves an image to a given storage path
///
/// - Parameters:
/// - image: The image to be saved
/// - imagePath: The image path where the image will be saved
private func saveImage(image:UIImage, imagePath:String) {

//Store image to the temporary folder for later use
var error: NSError?
var error: Error?

do {
try image.pngData()!.write(to: URL(fileURLWithPath: imagePath), options: [])
} catch let error1 as NSError {
} catch let error1 {
error = error1
if let actualError = error {
Swift.debugPrint("Image not saved. \(actualError)")
}
} catch {
fatalError()
}
}


private func readImage(imagePath:String, completion: @escaping (_ error:UIImage?) -> Void) -> Void {
var image:UIImage?
DispatchQueue.global(qos: .userInteractive).async {
Expand All @@ -109,16 +110,12 @@ public class LazyImage: NSObject {

image = UIImage(data:dat)
}
DispatchQueue.main.async {
completion(image)
}
completion(image)
}
}


//MARK: - Clear cache for specific URLs


/// Clear the storage for specific URLs if they are already downloaded
///
/// - Parameter urls: The urls array for which the storage will be cleared
Expand All @@ -135,26 +132,22 @@ public class LazyImage: NSObject {
let imageExists:Bool = FileManager.default.fileExists(atPath: imagePath)

if imageExists {
var error: NSError?
var error: Error?

do {
try FileManager.default.removeItem(atPath: imagePath)
} catch let error1 as NSError {
} catch let error1 {
error = error1
if let actualError = error {
Swift.debugPrint("Image not saved. \(actualError)")
}
} catch {
fatalError()
}
}
}
}


//MARK - Check image existence


/// Checks if image exists in storage
///
/// - Parameter url: The image URL
Expand All @@ -176,10 +169,8 @@ public class LazyImage: NSObject {
return imagePath
}


//MARK: - Preload setup image


/// Sets up the image before loading with a default image
private func setupImageBeforeLoading(imageView:UIImageView, defaultImage:String?) -> Void {

Expand All @@ -188,7 +179,6 @@ public class LazyImage: NSObject {
}
}


//MARK: - Setup 0 framed image

private func setUpZeroFramedImageIfNeeded(imageView:UIImageView) -> Void {
Expand All @@ -207,12 +197,10 @@ public class LazyImage: NSObject {
}
}


//MARK: - Image lazy loading

//MARK: Image lazy loading without completion


/// Downloads and shows an image URL to the specified image view
///
/// - Parameters:
Expand All @@ -225,7 +213,6 @@ public class LazyImage: NSObject {
self.load(imageView: imageView, url: url, defaultImage: nil) {_ in}
}


/// Downloads and shows an image URL to the specified image view presenting a spinner until the data are fully downloaded
///
/// - Parameters:
Expand All @@ -238,7 +225,6 @@ public class LazyImage: NSObject {
self.load(imageView: imageView, url: url, defaultImage: nil) {_ in}
}


/// Downloads and shows an image URL to the specified image view presenting a default image until the data are fully downloaded
///
/// - Parameters:
Expand All @@ -252,7 +238,6 @@ public class LazyImage: NSObject {
self.load(imageView: imageView, url: url, defaultImage: defaultImage) {_ in}
}


/// Downloads and shows an image URL to the specified image view presenting both a default image and a spinner until the data are fully downloaded
///
/// - Parameters:
Expand All @@ -266,10 +251,8 @@ public class LazyImage: NSObject {
self.load(imageView: imageView, url: url, defaultImage: defaultImage) {_ in}
}


//MARK: Image lazy loading with completion


/// Downloads and shows an image URL to the specified image view presenting a spinner until the data are fully downloaded
///
/// - Parameters:
Expand All @@ -288,7 +271,6 @@ public class LazyImage: NSObject {
}
}


/// Downloads and shows an image URL to the specified image view
///
/// - Parameters:
Expand All @@ -307,10 +289,8 @@ public class LazyImage: NSObject {
}
}


//MARK: Image lazy loading with completion and image resizing


/// Downloads and shows an image URL to the specified image view presenting a spinner until the data are fully downloaded.
/// The image is rescaled according to the size provided for better rendering
///
Expand All @@ -331,7 +311,6 @@ public class LazyImage: NSObject {
}
}


/// Downloads and shows an image URL to the specified image view.
/// The image is rescaled according to the size provided for better rendering
///
Expand All @@ -352,7 +331,6 @@ public class LazyImage: NSObject {
}
}


//MARK: Image lazy loading with force download, with completion and image resizing

/// Force downloads, even if cached, and shows an image URL to the specified image view presenting a spinner.
Expand All @@ -375,7 +353,6 @@ public class LazyImage: NSObject {
}
}


/// Force downloads, even if cached, and shows an image URL to the specified image view.
/// The image is rescaled according to the size provided for better rendering
///
Expand All @@ -396,11 +373,8 @@ public class LazyImage: NSObject {
}
}



//MARK: - Show Image


private func load(imageView:UIImageView, url:String?, defaultImage:String?, completion: @escaping (_ error:LazyImageError?) -> Void) -> Void {

self.setupImageBeforeLoading(imageView: imageView, defaultImage: defaultImage)
Expand Down Expand Up @@ -477,7 +451,6 @@ public class LazyImage: NSObject {
}
}


private func lazyLoad(imageView:UIImageView, url:String?, completion: @escaping (_ error:LazyImageError?) -> Void) -> Void {

if url == nil || url!.isEmpty {
Expand Down Expand Up @@ -526,7 +499,6 @@ public class LazyImage: NSObject {
}
}


//MARK: - Call

public func fetchImage(url:String?, completion: @escaping (_ image:UIImage?, _ error:LazyImageError?) -> Void) -> Void {
Expand Down Expand Up @@ -585,7 +557,6 @@ public class LazyImage: NSObject {
})
}


//MARK: Update the image

private func updateImageView(imageView:UIImageView, fetchedImage:UIImage?,
Expand Down Expand Up @@ -615,7 +586,6 @@ public class LazyImage: NSObject {
})
}


/****************************************************/
//MARK: - Show activity indicator

Expand All @@ -641,7 +611,6 @@ public class LazyImage: NSObject {
self.spinner = nil
}


/****************************************************/
//MARK: - Zoom functionality

Expand Down Expand Up @@ -711,8 +680,6 @@ public class LazyImage: NSObject {
})
}



@objc func zoomOutImageView(_ tap:UITapGestureRecognizer) -> Void {

UIApplication.shared.isStatusBarHidden = false
Expand All @@ -730,10 +697,8 @@ public class LazyImage: NSObject {
})
}



@objc func rotated()
{
@objc func rotated() {

self.removeZoomedImageView()

if(UIDevice.current.orientation.isLandscape)
Expand All @@ -748,7 +713,6 @@ public class LazyImage: NSObject {

}


func removeZoomedImageView() -> Void {

UIApplication.shared.isStatusBarHidden = false
Expand All @@ -766,11 +730,9 @@ public class LazyImage: NSObject {
}
}


/****************************************************/
//MARK: - Resize image


public func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {

let horizontalRatio:CGFloat = targetSize.width / image.size.width
Expand All @@ -785,7 +747,6 @@ public class LazyImage: NSObject {
return newImage!
}


/****************************************************/
//MARK: - Blur

Expand All @@ -805,5 +766,4 @@ public class LazyImage: NSObject {
imageView.addSubview(blurView)
return blurView
}

}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[![Version](https://img.shields.io/cocoapods/v/LazyImage.svg?style=flat)](https://cocoapods.org/pods/LazyImage)
[![License](https://img.shields.io/cocoapods/l/LazyImage.svg?style=flat)](https://cocoapods.org/pods/LazyImage)
[![Platform](https://img.shields.io/cocoapods/p/LazyImage.svg?style=flat)](https://cocoapods.org/pods/LazyImage)
[![Version](https://img.shields.io/cocoapods/v/LazyImage.svg?style=flat&logo=Swift)](https://cocoapods.org/pods/LazyImage)
[![License](https://img.shields.io/cocoapods/l/LazyImage.svg?style=flat&logo=Swift)](https://cocoapods.org/pods/LazyImage)
[![Platform](https://img.shields.io/cocoapods/p/LazyImage.svg?style=flat&logo=Swift)](https://cocoapods.org/pods/LazyImage)


### iOS - LazyImage
### LazyImage
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.0
Version 6.6.1


### Features
Expand Down Expand Up @@ -148,7 +148,7 @@ self.lazyImage.clearCacheForURLs(urls: urls)

```swift
self.lazyImage.fetchImage(url: url) {
[weak self] (image:UIImage?, error:LazyImageError?) in
(image:UIImage?, error:LazyImageError?) in
//image has the UIImage
}
```
Expand Down

0 comments on commit 3172d23

Please sign in to comment.