├── UIImage+AssetLaunchImage.podspec ├── .gitignore ├── README.md ├── LICENSE ├── UIImage+AssetLaunchImage.h └── UIImage+AssetLaunchImage.m /UIImage+AssetLaunchImage.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "UIImage+AssetLaunchImage" 3 | s.version = "1.0.1" 4 | s.summary = "Adds category methods to UIImage for simple access to launch image." 5 | s.homepage = "https://github.com/TimurBK/UIImage-XCAssets" 6 | s.license = { :type => 'MIT', :file => 'LICENSE' } 7 | s.author = 'Timur Kuchkarov' 8 | s.source = { :git => "https://github.com/TimurBK/UIImage-XCAssets.git", :tag => s.version.to_s } 9 | s.ios.deployment_target = '7.0' 10 | s.source_files = '*.{h,m}' 11 | s.requires_arc = true 12 | end -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | UIImage-XCAssets 2 | ================ 3 | 4 | Adds convenience methods to UIImage to get launch image if they are from xcassets. Also there are methods to get image generated from Launch XIB. 5 | 6 | Installation 7 | ================ 8 | 9 | This category is available via [CocoaPods](http://cocoapods.org/). Add the following line to your podfile: 10 | 11 | pod 'UIImage+AssetLaunchImage', '~> 1.0.1' 12 | 13 | Alternatively just drag&drop files to your project. 14 | 15 | 16 | For Swift bridge using “XXX-Bridging-Header.h”, add line bellow: 17 | 18 | #import -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Timur Kuchkarov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /UIImage+AssetLaunchImage.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+AssetLaunchImage.h 3 | // PaymentApp 4 | // 5 | // Created by Timur Kuchkarov on 25.11.14. 6 | // 7 | // The MIT License (MIT) 8 | // 9 | // Copyright (c) 2015 Timur Kuchkarov, i-2K 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 12 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 13 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 14 | // permit persons to whom the Software is furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 17 | // Software. 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface UIImage (AssetLaunchImage) 26 | 27 | /** 28 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method. 29 | * App should use Asset catalog or this method may return nil. 30 | * 31 | * @return Launch image with current status bar orientation. 32 | */ 33 | + (UIImage *)assetLaunchImage; 34 | 35 | /** 36 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method. 37 | * App should use IB based launch image or this method may return nil. 38 | * 39 | * @return Launch image with current status bar orientation. 40 | */ 41 | + (UIImage *)interfaceBuilderBasedLaunchImage; 42 | 43 | /** 44 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method if cache is YES 45 | * or via @code [UIImage imageWithContentsOfFile:] @endcode if cache is NO. 46 | * App should use IB based launch image or this method may return nil. 47 | * 48 | * @param orientation Image orientation to find. 49 | * @param cache If image should be cached by system means. 50 | * 51 | * @return Launch image with passed orientation. 52 | */ 53 | + (UIImage *)interfaceBuilderBasedLaunchImageWithOrientation:(UIInterfaceOrientation)orientation 54 | useSystemCache:(BOOL)cache; 55 | 56 | /** 57 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method if cache is YES 58 | * or via @code [UIImage imageWithContentsOfFile:] @endcode if cache is NO. 59 | * App should use Asset catalog or this method may return nil. 60 | * 61 | * @param orientation Image orientation to find. 62 | * @param cache If image should be cached by system means. 63 | * 64 | * @return Launch image with passed orientation. 65 | */ 66 | + (UIImage *)assetLaunchImageWithOrientation:(UIInterfaceOrientation)orientation useSystemCache:(BOOL)cache; 67 | 68 | /** 69 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method if cache is YES 70 | * or via @code [UIImage imageWithContentsOfFile:] @endcode if cache is NO 71 | * App should use Asset catalog or this method may return nil. 72 | * This method is useful for iOS 8 rotation methods where we get size app will transition to. 73 | * 74 | * @param size Size used to determine image orientation (if height > width - portrait, landscape left otherwise). 75 | * @param cache If image should be cached by system means. 76 | * 77 | * @return Launch image with constructed orientation. 78 | */ 79 | + (UIImage *)assetLaunchImageWithSize:(CGSize)size useSystemCache:(BOOL)cache; 80 | 81 | /** 82 | * Constructs launch image name and returns it via @code [UIImage imageNamed:] @endcode method if cache is YES 83 | * or via @code [UIImage imageWithContentsOfFile:] @endcode if cache is NO. 84 | * App should use Asset catalog or this method may return nil. 85 | * This method is useful for iOS 8 rotation methods where we get size app will transition to. 86 | * 87 | * @param size Size used to determine image orientation (if height > width - portrait, landscape left otherwise). 88 | * @param cache If image should be cached by system means. 89 | * 90 | * @return Launch image with constructed orientation. 91 | */ 92 | + (UIImage *)interfaceBuilderBasedLaunchImageWithSize:(CGSize)size useSystemCache:(BOOL)cache; 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /UIImage+AssetLaunchImage.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+AssetLaunchImage.m 3 | // PaymentApp 4 | // 5 | // Created by Timur Kuchkarov on 25.11.14. 6 | // 7 | // The MIT License (MIT) 8 | // 9 | // Copyright (c) 2015 Timur Kuchkarov, i-2K 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 12 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 13 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 14 | // permit persons to whom the Software is furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 17 | // Software. 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 19 | // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | #import "UIImage+AssetLaunchImage.h" 24 | 25 | // Thanks to http://stackoverflow.com/a/20045142/2082172 26 | // This category supports only iOS 7+, although it should be easy to add 6- support. 27 | 28 | static NSString *const kAssetImageBaseFileName = @"LaunchImage"; 29 | 30 | // Asset catalog part 31 | 32 | static CGFloat const kAssetImage4inchHeight = 568.; 33 | static CGFloat const kAssetImage35inchHeight = 480.; 34 | static CGFloat const kAssetImage6PlusScale = 3.; 35 | 36 | static NSString *const kAssetImageiOS8Prefix = @"-800"; 37 | static NSString *const kAssetImageiOS7Prefix = @"-700"; 38 | static NSString *const kAssetImagePortraitString = @"-Portrait"; 39 | static NSString *const kAssetImageLandscapeString = @"-Landscape"; 40 | static NSString *const kAssetImageiPadPostfix = @"~ipad"; 41 | static NSString *const kAssetImageHeightFormatString = @"-%.0fh"; 42 | static NSString *const kAssetImageScaleFormatString = @"@%.0fx"; 43 | 44 | // IB based part 45 | static NSString *const kAssetImageLandscapeLeftString = @"-LandscapeLeft"; 46 | static NSString *const kAssetImagePathToFileFormatString = @"~/Library/Caches/LaunchImages/%@/%@"; 47 | static NSString *const kAssetImageSizeFormatString = @"{%.0f,%.0f}"; 48 | 49 | @implementation UIImage (AssetLaunchImage) 50 | 51 | + (UIImage *)assetLaunchImageWithOrientation:(UIInterfaceOrientation)orientation useSystemCache:(BOOL)cache 52 | { 53 | UIScreen *screen = [UIScreen mainScreen]; 54 | CGFloat screenHeight = screen.bounds.size.height; 55 | if ([screen respondsToSelector:@selector(convertRect:toCoordinateSpace:)]) { 56 | screenHeight = [screen.coordinateSpace convertRect:screen.bounds toCoordinateSpace:screen.fixedCoordinateSpace] 57 | .size.height; 58 | } 59 | CGFloat scale = screen.scale; 60 | BOOL portrait = UIInterfaceOrientationIsPortrait(orientation); 61 | BOOL isiPhone = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone); 62 | BOOL isiPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad); 63 | NSMutableString *imageNameString = [NSMutableString stringWithString:kAssetImageBaseFileName]; 64 | if (isiPhone && 65 | screenHeight > kAssetImage4inchHeight) { // currently here will be launch images for iPhone 6 and 6 plus 66 | [imageNameString appendString:kAssetImageiOS8Prefix]; 67 | } else { 68 | [imageNameString appendString:kAssetImageiOS7Prefix]; 69 | } 70 | if (scale >= kAssetImage6PlusScale || isiPad) { 71 | NSString *orientationString = portrait ? kAssetImagePortraitString : kAssetImageLandscapeString; 72 | [imageNameString appendString:orientationString]; 73 | } 74 | 75 | if (isiPhone && screenHeight > kAssetImage35inchHeight) { 76 | [imageNameString appendFormat:kAssetImageHeightFormatString, screenHeight]; 77 | } 78 | 79 | if (cache) { 80 | if (isiPad) { 81 | [imageNameString appendString:kAssetImageiPadPostfix]; 82 | } 83 | return [UIImage imageNamed:imageNameString]; 84 | } else { 85 | if (scale > 1) { 86 | [imageNameString appendFormat:kAssetImageScaleFormatString, scale]; 87 | } 88 | if (isiPad) { 89 | [imageNameString appendString:kAssetImageiPadPostfix]; 90 | } 91 | NSData *data = 92 | [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNameString ofType:@"png"]]; 93 | return [UIImage imageWithData:data scale:scale]; 94 | } 95 | } 96 | 97 | + (UIImage *)interfaceBuilderBasedLaunchImageWithOrientation:(UIInterfaceOrientation)orientation 98 | useSystemCache:(BOOL)cache 99 | { 100 | UIScreen *screen = [UIScreen mainScreen]; 101 | CGFloat screenHeight = screen.bounds.size.height; 102 | CGFloat screenWidth = screen.bounds.size.width; 103 | BOOL portrait = UIInterfaceOrientationIsPortrait(orientation); 104 | if ((screenHeight > screenWidth && !portrait) || (screenWidth > screenHeight && portrait)) { 105 | CGFloat temp = screenWidth; 106 | screenWidth = screenHeight; 107 | screenHeight = temp; 108 | } 109 | NSMutableString *imageNameString = [NSMutableString stringWithString:kAssetImageBaseFileName]; 110 | NSString *orientationString = portrait ? kAssetImagePortraitString : kAssetImageLandscapeLeftString; 111 | [imageNameString appendString:orientationString]; 112 | 113 | [imageNameString appendFormat:kAssetImageSizeFormatString, screenWidth, screenHeight]; 114 | NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier]; 115 | NSString *pathToFile = [[NSString 116 | stringWithFormat:kAssetImagePathToFileFormatString, bundleID, imageNameString] stringByExpandingTildeInPath]; 117 | if (cache) { 118 | return [UIImage imageNamed:pathToFile]; 119 | } else { 120 | CGFloat scale = screen.scale; 121 | NSData *data = [NSData dataWithContentsOfFile:pathToFile]; 122 | return [UIImage imageWithData:data scale:scale]; 123 | } 124 | } 125 | 126 | + (UIImage *)interfaceBuilderBasedLaunchImage 127 | { 128 | UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 129 | return [UIImage interfaceBuilderBasedLaunchImageWithOrientation:statusBarOrientation useSystemCache:YES]; 130 | } 131 | 132 | + (UIImage *)assetLaunchImage 133 | { 134 | UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 135 | return [UIImage assetLaunchImageWithOrientation:statusBarOrientation useSystemCache:YES]; 136 | } 137 | 138 | + (UIImage *)assetLaunchImageWithSize:(CGSize)size useSystemCache:(BOOL)cache 139 | { 140 | UIInterfaceOrientation orientation = 141 | (size.height > size.width) ? UIInterfaceOrientationPortrait : UIInterfaceOrientationLandscapeLeft; 142 | return [UIImage assetLaunchImageWithOrientation:orientation useSystemCache:cache]; 143 | } 144 | 145 | + (UIImage *)interfaceBuilderBasedLaunchImageWithSize:(CGSize)size useSystemCache:(BOOL)cache 146 | { 147 | UIInterfaceOrientation orientation = 148 | (size.height > size.width) ? UIInterfaceOrientationPortrait : UIInterfaceOrientationLandscapeLeft; 149 | return [UIImage interfaceBuilderBasedLaunchImageWithOrientation:orientation useSystemCache:cache]; 150 | } 151 | 152 | @end 153 | --------------------------------------------------------------------------------