Skip to content

Commit

Permalink
Memory optimsation when loading images.
Browse files Browse the repository at this point in the history
Image data are now loaded on a background thread before been asigned on an image view.
  • Loading branch information
lamprosg committed Oct 18, 2018
1 parent ffa619e commit 251a08f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
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.5.1'
s.version = '6.5.2'
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
100 changes: 52 additions & 48 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.1
// Version 6.5.2


import Foundation
Expand Down Expand Up @@ -62,17 +62,17 @@ public class LazyImage: NSObject {
}

/*
private func SHA256(url:String) -> String {
let data = url(using: String.Encoding.utf8)
let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(((data! as NSData)).bytes, CC_LONG(data!.count), res?.mutableBytes.assumingMemoryBound(to: UInt8.self))
let hashedString = "\(res!)".replacingOccurrences(of: "", with: "").replacingOccurrences(of: " ", with: "")
let badchar: CharacterSet = CharacterSet(charactersIn: "\"<\",\">\"")
let cleanedstring: String = (hashedString.components(separatedBy: badchar) as NSArray).componentsJoined(by: "")
return cleanedstring
}
private func SHA256(url:String) -> String {
let data = url(using: String.Encoding.utf8)
let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(((data! as NSData)).bytes, CC_LONG(data!.count), res?.mutableBytes.assumingMemoryBound(to: UInt8.self))
let hashedString = "\(res!)".replacingOccurrences(of: "", with: "").replacingOccurrences(of: " ", with: "")
let badchar: CharacterSet = CharacterSet(charactersIn: "\"<\",\">\"")
let cleanedstring: String = (hashedString.components(separatedBy: badchar) as NSArray).componentsJoined(by: "")
return cleanedstring
}
*/

//MARK: - Image storage
Expand Down Expand Up @@ -100,17 +100,19 @@ public class LazyImage: NSObject {
}


private func readImage(imagePath:String) -> UIImage? {

//Read the image
private func readImage(imagePath:String, completion: @escaping (_ error:UIImage?) -> Void) -> Void {
var image:UIImage?
if let imageData = try? Data(contentsOf: URL(fileURLWithPath: imagePath)) {
//Image exists
let dat:Data = imageData

image = UIImage(data:dat)
DispatchQueue.global(qos: .userInteractive).async {
if let imageData = try? Data(contentsOf: URL(fileURLWithPath: imagePath)) {
//Image exists
let dat:Data = imageData

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


Expand Down Expand Up @@ -190,7 +192,7 @@ public class LazyImage: NSObject {
//MARK: - Setup 0 framed image

private func setUpZeroFramedImageIfNeeded(imageView:UIImageView) -> Void {

//Check if imageview size is 0
let width:CGFloat = imageView.bounds.size.width;
let height:CGFloat = imageView.bounds.size.height;
Expand Down Expand Up @@ -433,28 +435,30 @@ public class LazyImage: NSObject {
self.setUpZeroFramedImageIfNeeded(imageView: imageView)

//Try to read the image
let image = self.readImage(imagePath: imagePath)

if let image = image {
//Image read successfully
self.readImage(imagePath: imagePath) {
[weak self] (image:UIImage?) in

self.updateImageView(imageView:imageView, fetchedImage:image) {
if let image = image {
//Image read successfully

//Completion block
//Data available with no errors
completion(nil)
return
self?.updateImageView(imageView:imageView, fetchedImage:image) {

//Completion block
//Data available with no errors
completion(nil)
return
}
}
}
else {
//Image exists but corrupted. Load it again

//Lazy load image (Asychronous call)
self.lazyLoad(imageView: imageView, url: url) {
(error:LazyImageError?) in
else {
//Image exists but corrupted. Load it again

//Call completion block
completion(error)
//Lazy load image (Asychronous call)
self?.lazyLoad(imageView: imageView, url: url) {
(error:LazyImageError?) in

//Call completion block
completion(error)
}
}
}
}
Expand Down Expand Up @@ -567,7 +571,7 @@ public class LazyImage: NSObject {
Swift.debugPrint("Error : \(error!.localizedDescription)")
}
Swift.debugPrint("LazyImage: No image data available")

//No data available
let error: LazyImageError = LazyImageError.noDataAvailable
completion(nil, error)
Expand Down Expand Up @@ -698,7 +702,7 @@ public class LazyImage: NSObject {
UIView.animate(withDuration: 0.3, animations: {
imgV.frame=CGRect(x: 0,y: (screenBounds.size.height-image.size.height*screenBounds.size.width/image.size.width)/2, width: screenBounds.size.width, height: image.size.height*screenBounds.size.width/image.size.width)
self.backgroundView!.alpha=1;
},
},
completion: {(value: Bool) in
UIApplication.shared.isStatusBarHidden = true

Expand All @@ -718,11 +722,11 @@ public class LazyImage: NSObject {
UIView.animate(withDuration: 0.3, animations: {
imgV.frame = self.oldFrame
self.backgroundView!.alpha=0
},
completion: {(value: Bool) in
self.backgroundView!.removeFromSuperview()
self.backgroundView = nil
self.imageAlreadyZoomed = false //No more zoomed view
},
completion: {(value: Bool) in
self.backgroundView!.removeFromSuperview()
self.backgroundView = nil
self.imageAlreadyZoomed = false //No more zoomed view
})
}

Expand Down Expand Up @@ -753,7 +757,7 @@ public class LazyImage: NSObject {

UIView.animate(withDuration: 0.3, animations: {
bgView.alpha=0
},
},
completion: {(value: Bool) in
bgView.removeFromSuperview()
self.backgroundView = nil
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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.5.0
Version 6.5.2


### Features
Expand Down

0 comments on commit 251a08f

Please sign in to comment.