Skip to content

A pure swift library used for downloading images or JSON format files and with caching configuration support

License

Notifications You must be signed in to change notification settings

Sharkesm/MVDownloader

Repository files navigation

MVDownloader

Build Status Pod Version Pod Platform Pod License Repo Size

MVDownloader is a native swift library for making asynchronous remote requests to download images or JSON format files from the web.

Features

  • Asynchronous image downloading and caching.
  • Supports parallel remote requests and caches URL responses
  • Cancelable downloading and re-using resume data from previous requests
  • Fetching of images from the cache to improve performance and experience
  • Built-in transition animation when setting images
  • Supports image placeholder support
  • Guarantee same URL request won't be fired several times
  • Custom cache control and configuration
  • Guarantee invalid URL won't be fired
  • Guarantee main thread won't be blocked
  • Extends UIImageView and exposes usable methods that support a quick way to download images
  • Multiple resources accessing the same resource will be backed up with the same response
  • Supports decoding of instances of a data type from JSON objects.
  • Uses GRC under the hood

Example

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

Requirements

  • iOS 10.0+
  • Xcode 10.0+
  • Swift 5.0+

Dependencies

Installation

MVDownloader is available through CocoaPods. To install it, simply add the following line to your Podfile:

platform :ios, '10.0'
use_frameworks!
pod 'MVDownloader'

Usage

Shared Instance

MVDownloader has a shared instance that can be utilised to access all of the exposed API methods.

import MVDownloader 

MVDownloader.shared

By default the library will cache all responses from all given URL requests and utilises NSCache object to provide in-memory spacing of up to 80 mb. You can still configure a custom NSCache object with different storage capacity by initiating MVDownloader.init(urlCache:_) with defined cache.

Download Image

Downloading images using the library can be as easy as providing a proper URL request to requestImage(from:_, comepletion:_) method and takes over the process of downloading and converting response data to MVImage a type of UIImage.

import MVDownloader 

let url = URL(string:"https://techcrunch.com/wp-content/uploads/2015/04/codecode.jpg?w=1390&crop=1")!

MVDownloader.shared.requestImage(from: url) { (mvimage, error) in
    
    if let downloadedImage = mvimage {
       print("Downloaded image: ", downloadedImage)
    }
    
}

Also, there's another way to download images quickly without the hussle writting longer code blocks of such. Checkout mv_setImage example.

Download Image using mv_setImage

MVDownloader library under the hood it extends UIImageView and exposes mv_setUmage(from:_) method with intention of downloading images and setting them automatically and apply animation transition.

import MVDownloader

guard let url = URL(string:"https://techcrunch.com/wp-content/uploads/2015/04/codecode.jpg?w=1390&crop=1") else {
    print("Given url is invalid")
    return 
}

let imageView = UIImageView()

// Downloads image and sets it automatically 
imageView.mv_setImage(from: url) { (error) in 
  ...
}  

Download JSON

Decoding of instances of a particular type from JSON objects can be smootly executed by utilising MVDownloader ~ requestDecodable(type:_,from:) method.

import MVDownloader

/// A type that can convert itself into and out of an external representation.
public struct PhotoModel: Codable {
    var urls: PhotoUrls
}

public struct PhotoUrls: Codable {
    var raw: String
    var full: String
    var regular: String
    var small: String
    var thumb: String
}


guard let pasteBinUrl =
URL(string:"https://images.unsplash.com/photo-1464550883968-cec281c19761?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&w=400&fit=max&s=d5682032c546a3520465f2965cde1cec") else {
print("Given url is invalid")
return 
}

/// By default, below function utilises `JSONDecoder` object to decode instances of a data type from JSON objects.  

MVDownloader.shared.requestDecodable(type: [PhotoModel].self, from: pasteBinUrl) { (data, error) in

    ...
    // Proceed with data extraction 
    ...
}

Normal Remote Request

Another benefit for this library is that it's not limited to download JSON and Image only. But you can also still make normal requests to the network.

import MVDownloader 

let url = URL(string: "www.google.com")!
let urlRequest = URLRequest(url: url)

MVDownloader.shared.request(urlRequest) { (data, error) in
    ...
    // Process response data 
    ...
}

Cancel Active Request

Active remote request can still be cancelled and removed completely from active download task collection list.

import MVDownloader 

let url = URL(string: "www.google.com")!
let urlRequest = URLRequest(url: url)

let downloader = MVDownloader.shared 

// Active request on progress 
downloader.request(urlRequest) { (data, error) in 
    ...
    // Proceed with data extraction
    ...
}


// Cancelling active request using there associated `URL` request 
let requestDidCancel = downloader.cancelRequest(for: url)

// Check if request is successfully cancelled 
if requestDidCancel {
    ...
}

Author

Manase Michael, sharkesm@gmail.com

License

MVDownloader is available under the MIT license. See the LICENSE file for more info.

About

A pure swift library used for downloading images or JSON format files and with caching configuration support

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published