- Notifications
You must be signed in to change notification settings - Fork0
A thread safe, performant, feature rich image fetcher
License
dottnett/PINRemoteImage
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
PINRemoteImageManager is an image downloading, processing and caching manager. It uses the concept of download and processing tasks to ensure that even if multiple calls to download or process an image are made, it only occurs one time (unless an item is no longer in the cache). PINRemoteImageManager is backed byGCD and safe toaccess frommultiple threads simultaneously. It ensures that images are decoded off the main thread so that animation performance isn't affected. None of its exposed methods allow for synchronous access. However, it is optimized to call completions on the calling thread if an item is in its memory cache.
PINRemoteImage supports downloading many types of files. It, of course,supports bothPNGs andJPGs. It also supports decodingWebP images if Google's library is available. It even supportsGIFs andAnimated WebP via PINAnimatedImageView.
PINRemoteImage also has two methods to improve the experience of downloading images on slow network connections. The first is support forprogressive JPGs. This isn't any old support for progressive JPGs though: PINRemoteImage adds an attractive blur to progressive scans before returning them.
PINRemoteImageCategoryManager defines a protocol which UIView subclasses can implement and provide easy access toPINRemoteImageManager's methods. There arebuilt-in categories onUIImageView,PINAnimatedImageView andUIButton, and it's very easy to implement a new category. See [UIImageView+PINRemoteImage](/Pod/Classes/Image Categories/UIImageView+PINRemoteImage.h) of the existing categories for reference.
Objective-C
UIImageView *imageView = [[UIImageViewalloc]init];[imageViewpin_setImageFromURL:[NSURLURLWithString:@"http://pinterest.com/kitten.jpg"]];
Swift
letimageView=UIImageView()imageView.pin_setImage(from:URL(string:"https://pinterest.com/kitten.jpg")!)
Objective-C
UIImageView *imageView = [[UIImageViewalloc]init];[imageViewsetPin_updateWithProgress:YES];[imageViewpin_setImageFromURL:[NSURLURLWithString:@"http://pinterest.com/progressiveKitten.jpg"]];
Swift
letimageView=UIImageView()imageView.pin_updateWithProgress=trueimageView.pin_setImage(from:URL(string:"https://pinterest.com/progressiveKitten.jpg")!)
Objective-C
UIImageView *imageView = [[UIImageViewalloc]init];[imageViewpin_setImageFromURL:[NSURLURLWithString:@"http://pinterest.com/googleKitten.webp"]];
Swift
letimageView=UIImageView()imageView.pin_setImage(from:URL(string:"https://pinterest.com/googleKitten.webp")!)
Objective-C
PINAnimatedImageView *animatedImageView = [[PINAnimatedImageViewalloc]init];[animatedImageViewpin_setImageFromURL:[NSURLURLWithString:@"http://pinterest.com/flyingKitten.gif"]];
Swift
letanimatedImageView=PINAnimatedImageView()animatedImageView.pin_setImage(from:URL(string:"http://pinterest.com/flyingKitten.gif")!)
Objective-C
UIImageView *imageView = [[UIImageViewalloc]init];[self.imageViewpin_setImageFromURL:[NSURLURLWithString:@"https://i.pinimg.com/736x/5b/c6/c5/5bc6c5387ff6f104fd642f2b375efba3.jpg"]processorKey:@"rounded"processor:^UIImage *(PINRemoteImageManagerResult *result,NSUInteger *cost) { CGSize targetSize =CGSizeMake(200,300); CGRect imageRect =CGRectMake(0,0, targetSize.width, targetSize.height);UIGraphicsBeginImageContext(imageRect.size); UIBezierPath *bezierPath = [UIBezierPathbezierPathWithRoundedRect:imageRectcornerRadius:7.0]; [bezierPathaddClip]; CGFloat sizeMultiplier =MAX(targetSize.width / result.image.size.width, targetSize.height / result.image.size.height); CGRect drawRect =CGRectMake(0,0, result.image.size.width * sizeMultiplier, result.image.size.height * sizeMultiplier);if (CGRectGetMaxX(drawRect) >CGRectGetMaxX(imageRect)) { drawRect.origin.x -= (CGRectGetMaxX(drawRect) -CGRectGetMaxX(imageRect)) /2.0; }if (CGRectGetMaxY(drawRect) >CGRectGetMaxY(imageRect)) { drawRect.origin.y -= (CGRectGetMaxY(drawRect) -CGRectGetMaxY(imageRect)) /2.0; } [result.imagedrawInRect:drawRect]; UIImage *processedImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return processedImage; }];
Swift
letimageView=FLAnimatedImageView()imageView.pin_setImage(from:URL(string:"https://i.pinimg.com/736x/5b/c6/c5/5bc6c5387ff6f104fd642f2b375efba3.jpg")!, processorKey:"rounded"){(result, unsafePointer)->UIImage?inguardlet image= result.imageelse{returnnil}letradius:CGFloat=7.0lettargetSize=CGSize(width:200, height:300)letimageRect=CGRect(x:0, y:0, width: targetSize.width, height: targetSize.height)UIGraphicsBeginImageContext(imageRect.size)letbezierPath=UIBezierPath(roundedRect: imageRect, cornerRadius: radius) bezierPath.addClip()letwidthMultiplier:CGFloat= targetSize.width/ image.size.widthletheightMultiplier:CGFloat= targetSize.height/ image.size.heightletsizeMultiplier=max(widthMultiplier, heightMultiplier)vardrawRect=CGRect(x:0, y:0, width: image.size.width* sizeMultiplier, height: image.size.height* sizeMultiplier)if(drawRect.maxX> imageRect.maxX){ drawRect.origin.x-=(drawRect.maxX- imageRect.maxX)/2}if(drawRect.maxY> imageRect.maxY){ drawRect.origin.y-=(drawRect.maxY- imageRect.maxY)/2} image.draw(in: drawRect)UIColor.red.setStroke() bezierPath.lineWidth=5.0 bezierPath.stroke()letctx=UIGraphicsGetCurrentContext() ctx?.setBlendMode(CGBlendMode.overlay) ctx?.setAlpha(0.5)letlogo=UIImage(named:"white-pinterest-logo") ctx?.scaleBy(x:1.0, y:-1.0) ctx?.translateBy(x:0.0, y:-drawRect.size.height)iflet coreGraphicsImage= logo?.cgImage{ ctx?.draw(coreGraphicsImage, in:CGRect(x:90, y:10, width: logo!.size.width, height: logo!.size.height))}letprocessedImage=UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return processedImage}
Objective-C
[[PINRemoteImageManagersharedImageManager]setAuthenticationChallenge:^(NSURLSessionTask *task,NSURLAuthenticationChallenge *challenge, PINRemoteImageManagerAuthenticationChallengeCompletionHandler aCompletion) {aCompletion(NSURLSessionAuthChallengePerformDefaultHandling,nil)];
Swift
PINRemoteImageManager.shared().setAuthenticationChallenge{(task, challenge, completion)incompletion?(.performDefaultHandling,nil)}
Currently there are two ways PINRemoteImage is supporting high resolution images:
- If the URL contains an
_2x.or an_3x.postfix it will be automatically handled by PINRemoteImage and the resulting image will be returned at the right scale. - If it's not possible to provide an URL with an
_2x.or_3x.postfix, you can also handle it with a completion handler:
NSURL *url = ...;__weak UIImageView *weakImageView = self.imageView;[self.imageViewpin_setImageFromURL:urlcompletion:^(PINRemoteImageManagerResult * _Nonnull result) { CGFloat scale = UIScreen.mainScreen.scale;if (scale >1.0) { UIImage *image = result.image; weakImageView.image = [UIImageimageWithCGImage:image.CGImagescale:scaleorientation:image.imageOrientation]; }}];
// cache is an instance of PINCache as long as you haven't overridden defaultImageCachePINCache *cache = (PINCache *)[[PINRemoteImageManager sharedImageManager] cache];// Max memory cost is based on number of pixels, we estimate the size of one hundred 600x600 images as our max memory image cache.[[cache memoryCache] setCostLimit:600 * [[UIScreen mainScreen] scale] * 600 * [[UIScreen mainScreen] scale] * 100];// ~50 MB[[cache diskCache] setByteLimit:50 * 1024 * 1024];// 30 days[[cache diskCache] setAgeLimit:60 * 60 * 24 * 30];AddPINRemoteImage to yourPodfile and runpod install.
If you'd like to use WebP images, addPINRemoteImage/WebP to yourPodfile and runpod install.
Addgithub "pinterest/PINRemoteImage" to your Cartfile . SeeCarthage's readme for more information on integrating Carthage-built frameworks into your project.
Download the latest tag and drag thePod/Classes folder into your Xcode project. You must also manually link againstPINCache.
Install the docs by double clicking the.docset file underdocs/, or view them online atcocoadocs.org
You can set up PINRemoteImage as a submodule of your repo instead of cloning and copying all the files into your repo. Add the submodule using the commands below and then follow the manual instructions above.
git submodule add https://github.com/pinterest/PINRemoteImage.gitgit submodule update --initPINRemoteImage requires iOS 12.0 or greater.
Garrett Moon@garrettmoonPinterest
Copyright 2015 Pinterest, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License.
About
A thread safe, performant, feature rich image fetcher
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- Objective-C95.1%
- Swift3.2%
- Other1.7%
