Created
January 17, 2018 13:16
-
-
Save karosLi/ab130ea2cf0f9004a3626304401571c0 to your computer and use it in GitHub Desktop.
YYImage 支持传入 bundle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // YYImage+LKUIKit.m | |
| // LKUIKit | |
| // | |
| // Created by karos li on 2018/1/17. | |
| // Copyright © 2018年 karos. All rights reserved. | |
| // | |
| #import "YYImage+LKUIKit.h" | |
| /** | |
| An array of NSNumber objects, shows the best order for path scale search. | |
| e.g. iPhone3GS:@[@1,@2,@3] iPhone5:@[@2,@3,@1] iPhone6 Plus:@[@3,@2,@1] | |
| */ | |
| static NSArray *_LK_NSBundlePreferredScales() { | |
| static NSArray *scales; | |
| static dispatch_once_t onceToken; | |
| dispatch_once(&onceToken, ^{ | |
| CGFloat screenScale = [UIScreen mainScreen].scale; | |
| if (screenScale <= 1) { | |
| scales = @[@1,@2,@3]; | |
| } else if (screenScale <= 2) { | |
| scales = @[@2,@3,@1]; | |
| } else { | |
| scales = @[@3,@2,@1]; | |
| } | |
| }); | |
| return scales; | |
| } | |
| /** | |
| Add scale modifier to the file name (without path extension), | |
| From @"name" to @"name@2x". | |
| e.g. | |
| <table> | |
| <tr><th>Before </th><th>After(scale:2)</th></tr> | |
| <tr><td>"icon" </td><td>"icon@2x" </td></tr> | |
| <tr><td>"icon " </td><td>"icon @2x" </td></tr> | |
| <tr><td>"icon.top" </td><td>"icon.top@2x" </td></tr> | |
| <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr> | |
| <tr><td>"/path/" </td><td>"/path/" </td></tr> | |
| </table> | |
| @param scale Resource scale. | |
| @return String by add scale modifier, or just return if it's not end with file name. | |
| */ | |
| static NSString *_LK_NSStringByAppendingNameScale(NSString *string, CGFloat scale) { | |
| if (!string) return nil; | |
| if (fabs(scale - 1) <= __FLT_EPSILON__ || string.length == 0 || [string hasSuffix:@"/"]) return string.copy; | |
| return [string stringByAppendingFormat:@"@%@x", @(scale)]; | |
| } | |
| @implementation YYImage (LKUIKit) | |
| + (YYImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle { | |
| if (name.length == 0) return nil; | |
| if ([name hasSuffix:@"/"]) return nil; | |
| NSBundle *inBundle = bundle ? bundle : [NSBundle mainBundle]; | |
| NSString *res = name.stringByDeletingPathExtension; | |
| NSString *ext = name.pathExtension; | |
| NSString *path = nil; | |
| CGFloat scale = 1; | |
| // If no extension, guess by system supported (same as UIImage). | |
| NSArray *exts = ext.length > 0 ? @[ext] : @[@"", @"png", @"jpeg", @"jpg", @"gif", @"webp", @"apng"]; | |
| NSArray *scales = _LK_NSBundlePreferredScales(); | |
| for (int s = 0; s < scales.count; s++) { | |
| scale = ((NSNumber *)scales[s]).floatValue; | |
| NSString *scaledName = _LK_NSStringByAppendingNameScale(res, scale); | |
| for (NSString *e in exts) { | |
| path = [inBundle pathForResource:scaledName ofType:e]; | |
| if (path) break; | |
| } | |
| if (path) break; | |
| } | |
| if (path.length == 0) return nil; | |
| NSData *data = [NSData dataWithContentsOfFile:path]; | |
| if (data.length == 0) return nil; | |
| return [[self alloc] initWithData:data scale:scale]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment