Giter VIP home page Giter VIP logo

kfswiftimageloader's Introduction

KFSwiftImageLoader

KFSwiftImageLoader is an extremely high-performance, lightweight, and energy-efficient pure Swift async web image loader with memory and disk caching for iOS and  Watch.

This is the world's first  Watch-optimized async image loader with WKInterfaceImage extensions and intelligent automatic cache handling via WKInterfaceDevice.

Please also check out KFWatchKitAnimations for a great way to record beautiful 60 FPS animations for  Watch by recording animations from the iOS Simulator.

Note:

watchOS 3.0+ updates and example app coming soon as a minor version! (ETA: Mid-October). Please see the "watchos_3" branch for progress on the migration work being done.

Features

  • WKInterfaceImage, UIImageView, UIButton, and MKAnnotationView extensions for asynchronous web image loading.
  • Memory and disk cache to prevent downloading images every time a request is made or when the app relaunches, with automatic cache management to optimize resource use.
  • Energy efficiency by sending only one HTTP/HTTPS request for image downloads from multiple sources that reference the same URL string, registering them as observers for the request.
  • Maximum peformance by utilizing the latest and greatest of modern technologies such as Swift 3.0, URLSession, and GCD.

KFSwiftImageLoader Requirements

  • Xcode 8.0+
  • iOS 9.0+

CocoaPods

To ensure you stay up-to-date with the latest version of KFSwiftImageLoader, it is recommended that you use CocoaPods.

Optimized for CocoaPods 1.0+, so you will need to run the following command first:

sudo gem install cocoapods

Add the following to your Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'KFSwiftImageLoader', '~> 3.0'

You will need to import KFSwiftImageLoader everywhere you wish to use it:

import KFSwiftImageLoader

Example Usage

UIImageView

imageView.loadImage(urlString: urlString)

Yes, it really is that easy. It just works. In the above example, the inputs "placeholderImage" and "completionHandler" were ignored, so they default to nil. We can include them in the following way:

imageView.loadImage(urlString: urlString, placeholderImage: UIImage(named: "KiavashFaisali")) {
    success, error in
    
    // 'success' is a 'Bool' indicating success or failure.
    // 'error' is an 'NSError?' containing the error (if any) when 'success' is 'false'.
}

For flexibility, there are several different methods for loading images. Below are the method signatures for all of them:

func loadImage(urlString: String,
        placeholderImage: UIImage? = nil,
              completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil)

func loadImage(url: URL,
  placeholderImage: UIImage? = nil,
        completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil)

func loadImage(request: URLRequest,
      placeholderImage: UIImage? = nil,
            completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil)

WKInterfaceImage

interfaceImage.loadImage(urlString: urlString, placeholderImageName: "KiavashFaisali", shouldUseDeviceCache: true) {
    finished, error in
    
    // Completion handler called.
}

The main difference with the UIImageView extension is the parameter "shouldUseDeviceCache", which defaults to false if it is not explicitly set to true in the method.

"shouldUseDeviceCache" is a Bool indicating whether or not to use the  Watch's device cache for dramatically improved performance. This should only be considered for images that are likely to be loaded more than once throughout the lifetime of the app.

The magic here is that KFSwiftImageLoader will automatically and intelligently manage the  Watch's device cache. Should the image data be larger than the cache's size (5 MB), KFSwiftImageLoader will fallback to transmitting the data from the iPhone app's cache, or downloading the image and transmitting the downloaded data, depending on what's available.

UIButton

button.loadImage(urlString: urlString)

Again, KFSwiftImageLoader makes it very easy to load images. In this case, the button uses mostly the same method signature as UIImageView, but it includes two more optional parameters: "isBackgroundImage" and "forState".

func loadImage(urlString: String,
        placeholderImage: UIImage? = nil,
            controlState: UIControlState = .normal,
       isBackgroundImage: Bool = false,
              completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil)

"controlState" takes a UIControlState value that is required when setting images for buttons. "isBackgroundImage" simply indicates whether or not the button should use "setBackgroundImage:for:" or "setImage:for:" for image loading.

MKAnnotationView

annotationView.loadImage(urlString: string)

The methods in the MKAnnotationView extension are exactly the same as those in the UIImageView extension.

KFImageCacheManager

// Disable the fade animation.
// The default value is 0.1.
KFImageCacheManager.sharedInstance.fadeAnimationDuration = 0.0

// Set a custom timeout interval for the image requests.
// The default value is 60.0.
KFImageCacheManager.sharedInstance.timeoutIntervalForRequest = 15.0

// Set a custom request cache policy for the image requests as well as the session's configuration.
// The default value is .returnCacheDataElseLoad.
KFImageCacheManager.sharedInstance.requestCachePolicy = .useProtocolCachePolicy

// Disable file system caching by adjusting the max age of the disk cache and the request cache policy.
// The default value is 60 * 60 * 24 * 7 = 604800 seconds (1 week).
KFImageCacheManager.sharedInstance.diskCacheMaxAge = 0
KFImageCacheManager.sharedInstance.requestCachePolicy = .reloadIgnoringLocalCacheData

Sample App

Please download the sample app "SwiftImageLoader" in this repository for a clear idea of how to use KFSwiftImageLoader to load images for iOS and  Watch.

Contact Information

Kiavash Faisali

License

KFSwiftImageLoader is available under the MIT license.

Copyright (c) 2016 Kiavash Faisali

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

kfswiftimageloader's People

Contributors

kiavashfaisali avatar bassrock avatar

Stargazers

MohsinAli avatar

Watchers

MohsinAli avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.