Implementing iOS graphics in Xcode? - ios

I've started to create an iPhone game. I have created a background in a design application and set the initial size to the iPhone 5 dimensions. I have imported the image as #2x The code I have implemented is the following,
let backGround = SKSpriteNode(imageNamed: "Background")
backGround.position = CGPointZero
backGround.anchorPoint = CGPointZero
backGround.blendMode = SKBlendMode.Replace
self.size = CGSizeMake(backGround.size.width, backGround.size.height)
self.addChild(backGround)
Running in the simulator, the background displays in the correct size for the iPhone 5/5s and the iPhone 6/6 Plus. However, with the iPhone 4s and all iPad devices, The top half and bottom half of my image are cut off. How do i go about fixing this ?

Related

Circular UIImageView is square on some devices

I have a UIView that has a UIImageView in it.
I set the UIImageView to be circular and add a border to it like this:
self.profilePicImageView.layer.cornerRadius = self.profilePicImageView.frame.size.height / 2
self.profilePicImageView.layer.masksToBounds = true
self.profilePicImageView.layer.borderColor = UIColor.white.cgColor
self.profilePicImageView.layer.borderWidth = 3
It's being called after I initialize the view in my viewController and before I add it as a subview (function called setupUI()).
It works perfectly fine on most of the devices, but on the large screen devices (iPhone 6/6s/7 Plus and iPhone X) I get the border right but the image itself isn't circular.
Please see the examples:
Regular iPhones:
Large iPhones (iPhone 6/6s/7 Plus and iPhone X):
Any idea what the problem can be and how can I fix it?
Thanks!!
Just try this code it will help you.
imageView.layer.cornerRadius = imageHeight/2
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill

Screen hight with (UIScreen) is fine in iPad but wrong in iPhone

I am developing a game where a background is moving at the back. I sat the background hight using the below code. It is working fine with iPads but with iPhones it is not taking the proper hight of the screen; instead it is in the middle of the screen with almost half the hight of the screen! The application is only working in portrait orientation. This is for xCode 8 & SWIFT 3
Please check below the code I am using for the background
hight:
let screenSize: CGRect = UIScreen.main.fixedCoordinateSpace.bounds
background1.size.height = screenSize.height
Thanks in advance
I have resolved the issue but using a (workaround) rather than resolving the root cause as I could not identify it.
First I have noticed that when sizing the hight of the screen I have 2 conditions:
below code will work with all iPhones but not iPads:
background1.size.height = self.frame.height
below code will work with all iPads but not iPhones:
background1.size.height = screenSize.height
So since the minimum hight of screen of iPads is 1024; I used the below code to first check if it is iPad use condition 2 and if iPhone use condition 1 as follow:
let screenSize: CGRect = UIScreen.main.bounds
if screenSize.height < CGFloat(1024) {
background1.size.height = self.frame.height ////for iphone
} else {
background1.size.height = screenSize.height ////for ipad
}
I am not sure what your case is.But it happens in iPhone 5+ devices due to launch screen size.Normally it behaves like iPhone 5 screen size even with iPhone 6,iPhone 6+.
Now the soultion.
1.You can use the default launch screen storyboard to avoid the problem.
2.You have to add every sizes for iPhone screen size (320*480,640*960,640*1136,768*1334,1242*2208).please add also iPhone 7,7+ sizes too.
3.You can use asset Catalogs for launch screen.
If thats the case for you please use above solution.It should work.

iPhone 7 Plus AVPlayer has border around it (Colors mismatch on white)

I'm seeing strange behavior on the iPhone 7 Plus and iPhone 6 Plus. This doesn't happen on the simulator, only the physical device.
If you have an AVPlayer (Video has a white background) and the view to which it is attached has a white background (audio player is smaller than parent view) a border will appear around the AVPlayer.
The goal to do this was to blend the video into the background to create a cool effect. Its working great on every device except the physical Plus model devices.
My best guess is there is some perfect white difference. Does anyone know how to fix this or avoid this?
I had this exact problem and my solution was to add the AVPlayerLayer inside an UIView container and adding a mask onto the playerLayer with 1pt inset.
override func layoutSubview() {
super.layoutSubviews()
// .. sets frame to players source size
let maskLayer = playerLayer.mask ?? CALayer()
maskLayer.frame = playerLayer.bounds.insetBy(dx: 1, dy: 1)
maskLayer.backgroundColor = UIColor.white.cgColor
playerLayer.mask = maskLayer
}

Xcode asset catalog support for 3.5", 4", and 4.7" #2x images?

Xcode offers asset buckets for 1x, 2x, 4" 2x, and 3x assets. My fullscreen assets are all wonky with this configuration, because the 2x bucket is used for 3.5" and 4.7" screens. Currently I have a UIImage category that introspects the current screen size, and selects a "*-3.5" asset if the device seems to be 3.5".
This is clunky and not cool. Seeing that Xcode caters to all the different device sizes for their LaunchImage asset, I was hoping there was some way to supply device specific assets for non launch image assets without resorting to code.
I have reported a bug to Apple about this since mid November 2014 and I just noticed they marked it as No Value... which gives me the impression Apple has omitted an additional slot for iPhone 6 on purpose. My guess is they now want the 2x slot to be used for iPhone 6, and maybe they're reducing support to iPhone 4s as it's getting old.
If you really want to keep supporting the iPhone 4s, I'd suggest to use iPhone 6 sized images in the 2x slot, and then use the following method to load your images:
+(UIImage *)loadImageNamed:(NSString *)imageName
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat screenHeight = MAX(screenSize.width, screenSize.height);
CGFloat const IPHONE_4_SCREEN_HEIGHT = 480;
UIImage *image = [UIImage imageNamed:imageName];
if(screenHeight == IPHONE_4_SCREEN_HEIGHT) {
CGFloat const xScale = .85333333;//x conversion ratio from iPhone 6's 375 pts width screen to iPhone 4's 320 pts width screen
CGFloat const yScale = .71964018;//y conversion ratio from iPhone 6's 667 pts height screen to iPhone 4's 480 pts height screen
CGSize newSize = CGSizeMake(xScale * image.size.width, yScale * image.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
[image drawInRect:(CGRect){0, 0, newSize}];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
return image;
}
Resizing a big image (iPhone 6) to a smaller one (iPhone 4) doesn't lose much quality even if the aspect ration is not the same.
Using an existing project when moving to Xcode 6 and iOS 8 creates some initial issues. I solved this by changing the image set from Device specific to Universal. This removes the R4 image option and leaves you with #1x, #2xand #3x. If you use for example a background image that needs to fit the screen then I will recommend that you find an image that does not have to fit 100 % perfect to look good, and set image to be displayed as Aspect Fill.
I have the same problem and got confirmation from apple that I have to wrote code for that. For example you can try like this Fullscreen images on iPhone 6 with Asset Catalogs
But I actually went lazy about it by using one 2x for 4" & 4.7" screen and another 2x for 3.5" screen

Orientation problems with launch image in universal app

I have a Universal app with launch screens for iPhone and iPad, following Apple's naming convention for launch screens.
The app is only for Landscape orientation. When I run it on iPhone, the splash screen (which is 320 × 480 pixels) is rotated 90 degrees to the RIGHT, regardless of orientation.
Xcode won't let me rename the launch image to Default-LandscapeLeft.png as it's a Universal app. What can I do to correctly display the launch image?
Maybe you can provide two images, a Default.png for iPhone and Default-LandscapeLeft.png for iPad
Do you have portrait orientations disabled in the project settings?
I ended up fixing this by flipping the UIImageView:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *iphoneLaunchImage = [UIImage imageNamed:#"Default"];
initialImage = [[UIImageView alloc] initWithImage:iphoneLaunchImage];
//Flip it:
initialImage.transform = CGAffineTransformMakeScale(1, -1);
[self.view addSubview:initialImage];

Resources