Reputation: 119
Say if I have 3 animation files for a size for iPhone 5s, iPhone 6 and iPhone 6 Plus. What about ipad?
For the same view controller, how should I use these respective files for different iPhone sizes. Here's a specific example:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”iphone5s” ofType:@”gif”];
but what if I want to load for iPhone 6, iPhone 6 plus called iPhone6.gif
, iPhone6Plus.gif
etc
I am using Objective-C.
Upvotes: 0
Views: 151
Reputation: 2929
You can use SDVersion to detect the current device
information . SDVersion is :
So you can use it to get device model ,device screen size,device name,and iOS Version as follow:
// Check for device model
if ([SDVersion deviceVersion] == iPhone6)
NSLog(@"You got the iPhone 6. Sweet 🍭!");
else if ([SDVersion deviceVersion] == iPhone6Plus)
NSLog(@"iPhone 6 Plus? Bigger is better!");
else if ([SDVersion deviceVersion] == iPadAir2)
NSLog(@"You own an iPad Air 2 🌀!");
// Check for device screen size
if ([SDVersion deviceSize] == iPhone4inch)
NSLog(@"Your screen is 4 inches");
// Get device name
NSLog(@"%@", stringFromDeviceVersion([SDVersion deviceName]));
/* e.g: Outputs 'iPhone 6 Plus' */
// Check for iOS Version
if (iOSVersionGreaterThanOrEqual(@"8"))
NSLog(@"You are running iOS 8 or above!");
In your situation , you can:
NSString *name;
DeviceSize size = [SDVersion deviceSize];
if (size == iPhone35inch)
name = @"iPhone4";
else if(size == iPhone4inch)
name = @"iPhone5";
else if(size == iPhone47inch)
name = @"iPhone6";
else if(size == iPhone55inch)
name = @"iPhone6Plus";
else
name = @"iPad";
NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
Upvotes: 2
Reputation: 14128
Try using specific image file names with appropriate naming conventions. Like,
- fileName.png -> iPhone (non-retina/fallback)
- [email protected] -> iPhone (retina)
- [email protected] -> iPhone 6 Plus (retina)
- fileName~ipad.png -> iPad (non-retina)
- fileName@2x~ipad.png -> iPad (retina)
So, in code we can use:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”fileName” ofType:@”png”];
And internally OS
will detect the device version and pick the respective image to showcase.
I have used this using storyboard image assigning, I have not tried with codebase, but you can give a try.
Hope this helps.
Upvotes: 1