Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations

License

NotificationsYou must be signed in to change notification settings

JanGorman/MapleBacon

Repository files navigation

CIcodecov.ioVersionLicensePlatformCarthage compatibleSPM

Introduction

MapleBacon is a lightweight and fast Swift library for downloading and caching images.

Example

The folderExample contains a sample projects for you to try.

Requirements

  • Swift 5.1
  • iOS 10.0+
  • Xcode 10.2+

Installation

MapleBacon is available throughCocoaPods. To install add it to your Podfile:

pod"MapleBacon"

Carthage

github "JanGorman/MapleBacon"

andSwift Package Manager.

Usage

UIImageView

The most basic usage is via an extension onUIImageView. You pass it URL:

import MapleBaconprivatevarimageView:UIImageView!func someFunc(){leturl=URL(string:"")  imageView.setImage(with: url)}

If you want to add a placeholder while the image is downloading you specify that like this:

func someFunc(){leturl=URL(string:"")  imageView.setImage(with: url, placeholder:UIImage(named:"placeholder"))}

If your backend returns images that are not optimised for display, it's good practice to downsample them. MapleBacon comes with support for downsampling viadisplayOptions:

func someFunc(){leturl=URL(string:"")  imageView.setImage(with: url, displayOptions:.downsampled)}

Image Transformers

MapleBacon allows you to apply transformations to images and have the results cached so that you app doesn't need to perform the same work over and over. To make your own transformer, create a class conforming to theImageTransforming protocol. A transform can be anything you like, let's create one that applies a Core Image sepia filter:

privateclassSepiaImageTransformer:ImageTransforming{  // The identifier is used as part of the cache key. Make sure it's something uniqueletidentifier="com.schnaub.SepiaImageTransformer"func transform(image:UIImage)->UIImage?{letfilter=CIFilter(name:"CISepiaTone")!letciImage=CIImage(image: image)    filter.setValue(ciImage, forKey: kCIInputImageKey)    filter.setValue(0.5, forKey: kCIInputIntensityKey)letcontext=CIContext()guardlet outputImage= filter.outputImage,let cgImage= context.createCGImage(outputImage, from: outputImage.extent)else{return image}    // Return the transformed image which will be cached (or used by another transformer)returnUIImage(cgImage: cgImage)}}

You then pass this filter to MapleBacon in one of the convenience methods:

leturl=URL(string:"")lettransformer=SepiaImageTransformer()imageView.setImage(with: url, transformer: transformer)

If you want to apply multiple transforms to an image, you can chain your transformers:

letchainedTransformer=SepiaImageTransformer().appending(transformer:DifferentTransformer()).appending(transformer:AnotherTransformer())

Or if you prefer, using the custom>>> operator:

letchainedTransformer=SepiaImageTransformer()>>>DifferentTransformer()>>>AnotherTransformer()

(Keep in mind that if you are using Core Image it might not be optimal to chain individual transformers but rather create one transformer that applies multipleCIFilters in one pass. See theCore Image Programming Guide.)

Caching

MapleBacon will cache your images both in memory and on disk. Disk storage is automatically pruned after a week (taking into account the last access date as well) but you can control the maximum cache time yourself too:

letoneDaySeconds:TimeInterval=60*60*24MapleBacon.default.maxCacheAgeSeconds= oneDaySeconds

Combine

On iOS13 and above, you can useCombine to fetch images from MapleBacon

MapleBacon.shared.image(with: url).receive(on:DispatchQueue.main) // Dispatch to the right queue if updating the UI.sink(receiveValue:{ imagein    // Do something with your image}).store(in:&subscriptions) // Hold on to and dispose your subscriptions

Migrating from 5.x

There is a smallmigration guide in the wiki when moving from the 5.x branch to 6.x

License

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


[8]ページ先頭

©2009-2025 Movatter.jp