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

Overview

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:

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 5.1, URLSession, and GCD.

KFSwiftImageLoader Requirements

  • Xcode 11.0+
  • iOS 12.0+
  • watchOS 6.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.8.4+, so you will need to run the following command first:

sudo gem install cocoapods

Add the following to your Podfile

platform :ios, '12.0'

pod 'KFSwiftImageLoader', '~> 4.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 "placeholder" and "completion" were ignored, so they default to nil. We can include them in the following way:

imageView.loadImage(urlString: urlString, placeholder: UIImage(named: "KiavashFaisali")) {
    (success, error) in
    
    // 'success' is a 'Bool' indicating success or failure.
    // 'error' is an 'Error?' 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,
               placeholder: UIImage? = nil,
                completion: ((_ success: Bool, _ error: Error?) -> Void)? = nil)

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

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

WKInterfaceImage

func loadImage(_ urlString: urlString,
           placeholderName: String? = nil,
                completion: ((_ success: Bool, _ error: Error?) -> Void)? = nil)

The main difference with the UIImageView extension is the parameter "placeholderName", which requires the placeholder image to be bundled with the  Watch app for performance reasons.

UIButton

button.loadImage(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: "isBackground" and "controlState".

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

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

MKAnnotationView

annotationView.loadImage(urlString)

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.shared.fadeAnimationDuration = 0.0

// Set a custom timeout interval for the image requests.
// The default value is 60.0.
KFImageCacheManager.shared.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.shared.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.shared.diskCacheMaxAge = 0
KFImageCacheManager.shared.requestCachePolicy = .reloadIgnoringLocalCacheData

Sample App

Please take a look at the sample app under the Example folder for a clear idea of how to use KFSwiftImageLoader to load images in iOS and  Watch.

Contact Information

Kiavash Faisali

License

KFSwiftImageLoader is available under the MIT license.

Copyright (c) 2019 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.

Comments
  • Get from cache then network?

    Get from cache then network?

    How would i acheive this? I want to use cache to display whatever is available from cache on initial load, and then request the image from network if available - re-firing the same callback closure.

    opened by a1exb1 3
  • Swift 2 updated

    Swift 2 updated

    I just made a few updates in the code based on swift2 I know you will note merge this into master. Please, create a new branch for swiftv2 and I'll create a new pull request.

    opened by wmhass 2
  • WatchKit Not Available for iOS 13

    WatchKit Not Available for iOS 13

    Kiavash,

    I updated to XCode 11 for iOS 13 SDK support and now my app won't run. It is hung up on your WatchKit functions.

    Is there a quick fix for this?

    Thanks!

    opened by mreilly4 1
  • App submission - ITMS-90338: Non-public API usage

    App submission - ITMS-90338: Non-public API usage

    Hi,

    Since the release of watchOS 6 beta, I am receiving an invalid binary message every time I upload my app with the following description:

    ITMS-90338: Non-public API usage - The app contains or inherits from non-public classes in Frameworks/KFSwiftImageLoader.framework/KFSwiftImageLoader: WKInterfaceDevice, WKInterfaceImage . If method names in your source code match the private Apple APIs listed above, altering your method names will help prevent this app from being flagged in future submissions. In addition, note that one or more of the above APIs may be located in a static library that was included with your app. If so, they must be removed. If you think this message was sent in error and that you have only used Apple-published APIs in accordance with the guidelines, send the app's Apple ID, along with detailed information about why you believe the above APIs were incorrectly flagged, to [email protected]. For further information, visit the Technical Support Information at http://developer.apple.com/support/technical/

    opened by Niduank 1
  • How i can get the height and width of the image

    How i can get the height and width of the image

    I have to use height and width of the image to resize the uiimageview. but i am unable to get the height and width of the image.

    there is no option to get the height and width. below is my code. i want to assign image height and width instead of static values (width: 355, height: 200)

    objImgview.loadImageFromURLString(strURLN, placeholderImage: UIImage(named: "KiavashFaisali")) { (finished, potentialError) in if finished { objImgview.frame = CGRect(x: 0, y: CGFloat(imgPosteryPoss), width: 355, height: 200 ) } else if let error = potentialError { print("error occurred with description: (error.localizedDescription)") } }

    opened by moinmail 1
  • Memory leak

    Memory leak

    I ran the memory leak instrument over my app which uses this image loader. What I found is that this line of code sharedURLCache.removeCachedResponseForRequest(request) is frequently leaking memory.

    opened by ValiDraganescu 1
  • Managing Memory

    Managing Memory

    Hi,

    All the images are placed in memory but we have no methods to clean or set the cache size. So when browsing long list of imageviews, the memory is increasing more and more (until we reach a memory warning). Will it be possible to have a method to set the cache memory size OR to clean the memory.

    Thanks Christophe

    opened by csurbier 1
Releases(4.0.0)
Owner
Kiavash Faisali
Passionate about breakthrough, innovative solutions to everyday problems. Not very interested in the status quo.
Kiavash Faisali
Lightweight and customisable async image loading in SwiftUI. Supports on-disk storage, placeholders and more!

Asyncrounously download and display images in Swift UI. Supports progress indicators, placeholders and image transitions. RemoteImageView Asyncrounous

Callum Trounce 192 Dec 7, 2022
A pure Swift high-performance asynchronous image loading framework. SwiftUI supported.

Longinus Longinus is a pure-Swift high-performance asynchronous web image loading,caching,editing framework. It was learned from Objective-C web image

Qitao Yang 290 Dec 17, 2022
Async image downloader with Mem&Disk cached as a UIImageView extension

HBWebImage Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation HBWebIm

haoboxuxu 1 Oct 22, 2022
Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web

Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web. It provides you a chance to use a pure-Swift way to work

Wei Wang 20.9k Dec 30, 2022
A lightweight and fast image loader for iOS written in Swift.

ImageLoader ImageLoader is an instrument for asynchronous image loading written in Swift. It is a lightweight and fast image loader for iOS. Features

Hirohisa Kawasaki 293 Nov 24, 2022
Twitter Image Pipeline is a robust and performant image loading and caching framework for iOS clients

Twitter Image Pipeline (a.k.a. TIP) Background The Twitter Image Pipeline is a streamlined framework for fetching and storing images in an application

Twitter 1.8k Dec 17, 2022
🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations

MapleBacon Introduction MapleBacon is a lightweight and fast Swift library for downloading and caching images. Example The folder Example contains a s

Jan Gorman 335 Nov 1, 2022
AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.

URLImage URLImage is a SwiftUI view that displays an image downloaded from provided URL. URLImage manages downloading remote image and caching it loca

Dmytro Anokhin 1k Jan 4, 2023
Very easy to use image loader for swift

Silo Silo is a extremely easy to use and very basic image loader for iOS built in Swift. if you use Silo in your project please let me know! Version 2

Jose Quintero 17 Dec 17, 2022
🚀SwiftUI Image downloader with performant LRU mem/disk cache.

Progressive concurrent image downloader for SwiftUI, with neat API and performant LRU mem/disk cache.

Cheng Zhang 42 Sep 24, 2022
High-performance animated GIF support for iOS in Swift

Gifu adds protocol-based, performance-aware animated GIF support to UIKit. (It's also a prefecture in Japan). Install Swift Package Manager Add the fo

Reda Lemeden 2.6k Jun 21, 2021
APNGKit is a high performance framework for loading and displaying APNG images in iOS and macOS.

APNGKit is a high performance framework for loading and displaying APNG images in iOS and macOS. It's built on top of a modified version of libpng wit

Wei Wang 2.1k Dec 30, 2022
High performance GIF engine

SwiftyGif High performance & easy to use Gif engine Features UIImage and UIImageView extension based Remote GIFs with customizable loader Great CPU/Me

Alexis Creuzot 1.7k Jan 3, 2023
AYImageKit is a Swift Library for Async Image Downloading, Show Name's Initials and Can View image in Separate Screen.

AYImageKit AYImageKit is a Swift Library for Async Image Downloading. Features Async Image Downloading. Can Show Text Initials. Can have Custom Styles

Adnan Yousaf 11 Jan 10, 2022
Animated sharingan loader for iOS projects.

Animated sharingan loader for iOS projects. This is a fun project created using SwiftUI with love for the Uchiha clan. More types of eyes to come with tons of customizations...

Manas Aggarwal 16 Nov 30, 2022
A Swift/SwiftUI utility for caching and displaying images in SwiftUI Views

A Swift/SwiftUI utility for caching and displaying images asynchronously. Built with Swift 5.5 and works with async/await.

王雪铮 Xuezheng Wang 1 May 5, 2022
An implementation of High Pass Skin Smoothing using Apple's Core Image Framework

YUCIHighPassSkinSmoothing An implementation of High Pass Skin Smoothing using CoreImage.framework Available on both OS X and iOS. Ports A MetalPetal b

Yu Ao 1.2k Dec 17, 2022
High Quality Image ScrollView using cropped tiled images.

THTiledImageView Feature ?? THTiledImageView fully support UIScrollView. You can subclass it and use it. ?? Support Async Image Downloading & Caching.

null 28 Oct 28, 2022
Advanced framework for loading, caching, processing, displaying and preheating images.

Advanced framework for loading, caching, processing, displaying and preheating images. This framework is no longer maintained. Programming in Swift? C

Alexander Grebenyuk 1.2k Dec 23, 2022