简体   繁体   中英

Setting UIImage to fit iPhone5 and iPhone6

We would like to set in code, an image to fit both iPhone5 and iPhone6.

We have an image, that its size is ready for iPhone6 , and we would like it to be scaled and placed on the same position on iPhone5 .

So ,the next code,is working good on iPhone6 , but on the iPhone5 the image is not scaled, i guess because i take the size of the actual image , and not the ratio to the screen size : (i also have launch images on the app)

 UIImage *imgt=[UIImage imageNamed:@"title.png"];
    UIImageView *title=[[UIImageView alloc] initWithFrame:CGRectMake((width-imgt.size.width)/2.0,imgt.size.height/2.0, imgt.size.width, imgt.size.height)];
    title.image=imgt;
    [self.view addSubview:title];

Question is, how can i get the scaled image size on the iPhone5s ? what he does is to take the actual size(the big one of iPhone6) , but because i know that the image size is scaled, i was expecting to get the scaled size with imgt.size.width

You would have to make a separate image that is scaled to the iPhone 5/5s. You could do this in Photoshop or some other editing program. After making the scaled image you could set up a conditional statement to check if the device the user is using. I'm assuming you know how to do this, but I'll put some code down below anyways.

To Check Which Device A User is Using

#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString *) platform {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *device = malloc(size);
sysctlbyname("hw.machine", device, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(device);
return platform;
}

This would make a NSString that could be used to determine the device. You could then set up an if then statement with it. You could check the platform string against another device string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM