Coder Social home page Coder Social logo

uikit0 / dfimagemanager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kean/dfimagemanager

0.0 0.0 0.0 1.85 MB

Modern iOS framework for fetching images from various sources. Delivers consistency, extensibility, and performance.

License: MIT License

Swift 0.19% Ruby 0.58% Objective-C 99.23%

dfimagemanager's Introduction

DFImageManager

Modern iOS framework for fetching, caching, processing, displaying and preheating images from various sources. It uses latest advancements in iOS SDK and doesn't reinvent existing technologies. It provides a powerful API that will extend the capabilities of your app.

The DFImageManager has a single responsibility of providing a great API for managing image requests, with an ability to easily plug-in everything else that your application might need. It also features multiple subspecs that integrate things like AFNetworking as a networking stack for fetching images, and FLAnimatedImage as a performant animated GIF engine.

Supported Resources

  • NSURL with http, https, ftp, file, and data schemes (AFNetworking or NSURLSession subspec)
  • PHAsset, NSURL with com.github.kean.photos-kit scheme (PhotosKit subspec)
  • DFALAsset, ALAsset, NSURL with assets-library scheme (AssetsLibrary subspec)

Features

  • Zero config, yet immense customization and extensibility.
  • Uses latest advancements in Foundation URL Loading System including NSURLSession that supports SPDY protocol.
  • Instead of reinventing a caching methodology it relies on HTTP cache as defined in HTTP specification and caching implementation provided by Foundation URL Loading System. The caching and revalidation are completely transparent to the client.
  • Has basic built-in networking implementation, and optional AFNetworking integration which should be your primary choice. Combine the power of both frameworks!
  • Animated GIF support using best-in-class FLAnimatedImage library.
  • Common APIs for different resources (NSURL, PHAsset, ALAsset, and your custom classes).
  • Centralized image decompression, resizing and processing. Fully customizable.
  • Separate memory cache for decompressed and processed images. Fine grained control over memory cache.
  • Compose image managers into a tree of responsibility.
  • Automatic preheating of images that are close to the viewport.
  • Groups similar requests and never executes them twice. Intelligent control over which requests are considered equivalent (both in terms of fetching and processing).
  • High quality code base. Dependency injection is used throughout.
  • Extreme performance even on outdated devices. Asynchronous and thread safe.
  • Unit tested.

Getting Started

  • Download the latest release version
  • Take a look at the comprehensive demo, it's easy to install with pod try DFImageManager command
  • Check out the complete documentation
  • View the growing project Wiki
  • Experiment with the APIs in a Swift playground available in the project
  • Install using CocoaPods, import <DFImageManager/DFImageManagerKit.h> and enjoy!

Requirements

iOS 7.0+

Usage

Zero config image fetching

DFImageRequestID *requestID = [[DFImageManager sharedManager] requestImageForResource:[NSURL URLWithString:@"http://..."] completion:^(UIImage *image, NSDictionary *info) {
  // Use decompressed image and inspect info
}];

[requestID cancel]; // requestID can be used to cancel the request

Add request options

DFImageRequestOptions *options = [DFImageRequestOptions new];
options.allowsClipping = YES;
options.progressHandler = ^(double progress){
  // Observe progress
};

NSURL *imageURL = [NSURL URLWithString:@"http://..."];
[[DFImageManager sharedManager] requestImageForResource:imageURL targetSize:CGSizeMake(100.f, 100.f) contentMode:DFImageContentModeAspectFill options:options completion:^(UIImage *image, NSDictionary *info) {
  // Image is resized and clipped to fill 100x100px square
}];

Options can be specialized and packed into DFImageRequest

Use DFURLRequestCachePolicyKey to set a request cache policy. Create instance of DFImageRequest to pack all request parameters.

DFImageRequestOptions *options = [DFImageRequestOptions new];
options.userInfo = @{ @"DFURLRequestCachePolicyKey" : @(NSURLRequestReturnCacheDataDontLoad) };
    
DFImageRequest *request = [[DFImageRequest alloc] initWithResource:[NSURL URLWithString:@"http://..."] targetSize:CGSizeMake(100.f, 100.f) contentMode:DFImageContentModeAspectFill options:options];
    
[[DFImageManager sharedManager] requestImageForRequest:request completion:^(UIImage *image, NSDictionary *info) {
  // Image is resized and clipped to 100x100 px square
}];

Start multiple requests with a single completion handler

The DFImageFetchTask class manages execution of one or many image requests. It also stores execution state for each request.

DFImageRequest *previewRequest = [DFImageRequest requestWithResource:[NSURL URLWithString:@"http://preview"]];
DFImageRequest *fullsizeRequest = [DFImageRequest requestWithResource:[NSURL URLWithString:@"http://fullsize_image"]];

NSArray *requests = @[ previewRequest, fullsizeRequest ];
DFImageFetchTask *task = [DFImageFetchTask requestImageForRequests:requests handler:^(UIImage *image, NSDictionary *info, DFImageRequest *request) {
  // Handler is called at least once
  // For more info see DFImageFetchTask class
}];

// Track the state of the requests
DFImageFetchContext *context = [task contextForRequest:previewRequest];
BOOL isPreviewFetched = context.image != nil;

There are many ways how composite requests can be used.

Use UI components

Use methods from UIImageView category for simple cases:

UIImageView *imageView = ...;
[imageView df_setImageWithResource:[NSURL URLWithString:@"http://..."]];

Use DFImageView for more advanced features:

DFImageView *imageView = ...;
imageView.allowsAnimations = YES; // Animates images when the response isn't fast enough
imageView.allowsAutoRetries = YES; // Retries when network reachability changes

[imageView prepareForReuse];
[imageView setImageWithResource:[NSURL URLWithString:@"http://..."]];
// Or use other APIs, for example, set multiple requests [imageView setImageWithRequests:@[ ... ]];

Use the same DFImageManaging API for PHAsset, ALAsset and your custom classes

PHAsset *asset = ...;
[[DFImageManager sharedManager] requestImageForResource:asset targetSize:CGSizeMake(100.f, 100.f) contentMode:DFImageContentModeAspectFill options:nil completion:^(UIImage *image, NSDictionary *info) {
  // Image resized to 100x100px square
  // Photos Kit image manager does most of the hard work
}];
NSURL *assetURL = [NSURL df_assetURLWithAsset:asset];
    
// There are Photos Kit specific options as well
DFPhotosKitImageRequestOptions *options = [DFPhotosKitImageRequestOptions new];
options.version = PHImageRequestOptionsVersionUnadjusted;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

DFImageRequest *request = [[DFImageRequest alloc] initWithResource:assetURL targetSize:DFImageMaximumSize contentMode:DFImageContentModeAspectFill options:options];

Use composite managers

The DFCompositeImageManager allows clients to construct a tree of responsibility from multiple image managers, where image requests are dynamically dispatched between them. Each manager should conform to DFImageManaging protocol. The DFCompositeImageManager also conforms to DFImageManaging protocol, which lets clients treat individual objects and compositions uniformly. The default [DFImageManager sharedManager] is a composite that contains all built in managers: the ones that support NSURL fetching, PHAsset objects, etc.

It's easy for clients to add additional managers to the shared manager. You can either add support for new image requests, or intercept existing onces. For more info see Composing Image Managers.

// Implement custom image fetcher that conforms to DFImageFetching protocol,
// including - (BOOL)canHandleRequest:(DFImageRequest *)request; method
id<DFImageFetching> fetcher = [YourImageFetcher new];
id<DFImageProcessing> processor = [YourImageProcessor new];
id<DFImageCaching> cache = [YourImageMemCache new];

// Create DFImageManager with your configuration.
DFImageManagerConfiguration *configuration = [DFImageManagerConfiguration configurationWithFetcher:fetcher processor:processor cache:cache];
id<DFImageManaging> manager = [[DFImageManager alloc] initWithConfiguration:configuration];

// Create composite manager with your custom manager and all built-in managers.
NSArray *managers = @[ manager, [DFImageManager sharedManager] ];
id<DFImageManaging> compositeImageManager = [[DFCompositeImageManager alloc] initWithImageManagers:managers];

// Use dependency injector to set shared manager
[DFImageManager setSharedManager:compositeImageManager];

What's more

Those were the most common cases. DFImageManager is packed with other features. For more info check out the complete documentation and project Wiki

Installation with CocoaPods

CocoaPods is the dependency manager for Cocoa projects, which automates the process of integrating third-party frameworks like DFImageManager. If you are not familiar with CocoaPods the best place to start would be official CocoaPods guides. To install DFImageManager add a dependency in your Podfile:

# Podfile
platform :ios, '7.0'
pod 'DFImageManager'

By default it will install subspecs:

  • DFImageManager/Core - core DFImageManager classes
  • DFImageManager/UI - UI components
  • DFImageManager/NSURLSession - basic networking on top of NSURLSession
  • DFImageManager/PhotosKit - Photos Framework support
  • DFImageManager/AssetsLibrary - ALAssetsLibrary support

There are two more optional subspecs:

  • DFImageManager/AFNetworking - replaces networking stack with AFNetworking
  • DFImageManager/GIF - GIF support with a FLAnimatedImage dependency

To install optional dependencies include them in your Podfile:

# Podfile
platform :ios, '7.0'
pod 'DFImageManager'
pod 'DFImageManager/AFNetworking'
pod 'DFImageManager/GIF'

Contribution

  • If you need help, use Stack Overflow. (Tag 'dfimagemanager')
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, and can provide steps to reproduce it, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

DFImageManager is constantly improving. Help to make it better!

Contacts

License

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

dfimagemanager's People

Contributors

jeffreyjackson avatar kean 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.