iphone app background image stretched in 3.5 simulator - ios

I am creating an app with a single view. The app has a background image. In my code I use:
#define deviceIsIPhone5 ([UIScreen mainScreen].applicationFrame.size.height == 568 ? YES : NO)
Then in my viewDidLoad:
if (deviceIsIPhone5)
{
backgroundImage.image = [UIImage imageNamed:#"Background-568h.png"];
}
else
{
backgroundImage.image = [UIImage imageNamed:#"Background.png"];
}
Background.png is 640x960
Background-658h.png is 640x1136
Works fine with 4" simulator but when I use 3.5" simulator the bottom of the picture is cut out, as if it is being stretched...
Am I doing anything wrong?

As per your comment, NSLog is indicating that the size of the ImageView's frame extends below the bottom of the screen.
The reported frame of the image view is:
(0 0; 320 568)
The image looks stretched because it is being stretched to fill a frame extending off the screen. You need to make sure your image view is sized appropriately depending on the screen size.

Set autoresing mask of your image view as shown in figure.

Related

How to config proper view size with UIImageView and UIImage?

I'm new to iOS programming, now I'm writing a camera app using AVFoundation. My problem is I have to preview the image just taken at full screen size, but no matter how I tried, I just got a clipped image (top left proportion of the original image) displayed full screen size on the phone. My image just taken is at 1920 * 1080 res, my UIImageView is set to AspectFit, and on the storyboard, it appears at full screen size.
The code to display the image is like,
[self.imageView setFrame:self.view.bounds];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit] ;
[self.imageView setImage:image] ;
[self.view insertSubview:_imageView aboveSubview:_previewView] ;
[self.view bringSubviewToFront:_stillButton] ;
[self.view bringSubviewToFront:_maskButton] ;
[self.view bringSubviewToFront:_confirmButton] ;
And when I observe the size of the UIImageView object self.imageView, the size is not at the screen size, but just 320*568. However, the previewView which is a AVFoundation camera shooting preview object, it is also at 320*568, but it appears the previewView size is correct from the looking of the running app. and at last, the self.view.frame.size is also 320*568.
So my problem is how to view the image taken at 1920*1080 at full screen size?
try this:
imageView.contentMode = UIViewContentModeCenter

Background image to fit 3.5" and 4" screens

I have been looking online for a while now on how to make my images fit the iPhones 3.5" screen and 4" screen but i cant find anything.
I have a -568#2x.png image, a image.png, and a image#2x.png but when i run the simulator for the
iPhone 3.5" the image is stretched to far to the right.
I need help to fix this, please.
In terms of a Background image. Just add a UIImageView and which displays an image depending on screen size. (You will need to make two sets of images for this so you don't get any stretching).
You can then set which image to be displayed in the UIImageView by adding a simple if statement to check which screen size you have in the viewDidLoad method like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height == 480) {
// 3.5 inch display
[background_image setImage:[UIImage imageNamed:#"small_background.png"]];
}
else if (result.height == 568) {
// 4 inch display
[background_image setImage:[UIImage imageNamed:#"big_background.png"]];
}
}
Don't forget to declare "background_image" as a UIImageView in your header file:
IBOutlet UIImageView *background_image;

UIImage screen size condition weird behaviour

So I need to differentiate the screen size (3.4 or 4 inch) devices in order to select the right image to display.
Here is what I did: -- from here
#define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
if(IS_PHONEPOD5()) {
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay-568h#2x.png"];
} else {
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay.png"];
}
Regardless of the screen size the image view always contains the larger image and extends off the screen (on the 3.5")
Am I doing something wrong, I have tried a few other things and it has always done the same thing.
EDIT: Even if I don't ever select the larger image it still is the one on screen at runtime:
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay.png"];
It is still the larger image?
Have you checked UIViewContentMode of UIImageView? Try it by setting UIViewContentModeScaleAspectFit and check if it works.
don't use #2x in your image string. because every retina device will automatically take care of that.
use should save your image text like image#2x.png but i must use only without #2x extention. use properly like [UIImage imageNamed:#"image.png"]; this is for 3.5 inch.
How to use 4 inch screen use extention with -568h. Eg:
image-568h#2x.png. use like [UIImage imageNamed:#"image-568h.png"];
self.tutorialImageView.image = [UIImage imageNamed:#"tutorialOverlay-568h.png"];
more detail
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/App-RelatedResources/App-RelatedResources.html
Edited:
self.navigationcontroller.navigationbar.translucent = NO;

Handling different iphone screen sizes/resolution for background images

I would like to better understand the iphone resolutions etc.
I have an application that has a basic buttonView and logoView. I have output the height of the logoView which will auto fit in height depending on screen size.
For the iphone5 I have 318 to work with.
For the iphone4(<) I have 230 to work with.
My question is, how should I handle the image used for the background of this view. Would I create one three separate images for the following?
-iphone3 etc (230)
-iphone4 retina (230 size, #2)
-iphone5 retina (328 size, #2)
Or would I create only the 2x 230 images, and can I stretch the image to 318 when an iphone5 is used and more space is available?
It all depends on your image:
If your Image can be stretched, UIImageView will do all the work.
If only a part of you image should be stretched you should use this:
imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
If your image can't be stretched you should then do different images for the phones and change them in runtime.
UPDATE
For the last point you could do something like this in your viewDidLoad method:
BOOL isIPhone = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
BOOL isIPhone5 = isIPhone && ([[UIScreen mainScreen] bounds].size.height > 480.0);
if (isIPhone5) {
imageView.image = [UIImage imageNamed:#"iphone4image.png"];
} else {
imageView.image = [UIImage imageNamed:#"iphone5image.png"];
}
iOS 8 has different size classes for different screens. It's explained very well here. Every iOS Developer should go through this link:
If you want to make this a little more succinct you can account for longer screen sizes with a macro.
#define ASSET_BY_SCREEN_HEIGHT(regular, longScreen) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? regular : longScreen)
Example usage:
- (void)viewDidLoad
{
[super viewDidLoad];
self.splashScreenImageView.image = [UIImage imageNamed:ASSET_BY_SCREEN_HEIGHT(#"Default", #"Default-568h")];
}

UIImgePickerView Images taken in landscape stretched when displayed

In the UIImagePicker the user takes photos, then the photos are saved, and loaded into a tableview, now when the images are taken normally (portrait) they are a perfect size in the image view because this is how I set it. But when the user takes an image with the device in landscape, the image looks skewed and looks very distorted. (See picture)
Top is portrait picture, bottom is landscape
So does anyone have any suggestions on how this can be done?
Any help would be amazing
If it is taken in Landscape, then you show it another frame with size width > height. Or try using the following code and see if it improves something.
imageViewTemp.clipsToBounds = YES;
imageViewTemp.contentMode = UIViewContentModeScaleAspectFit;
The UIImageView stretches images to fit its size. This is what you are seeing. To stop it, you might change the size of the image view, like so:
CGRect newFrame = CGRectMake(imageView.frame.origin.x,
imageView.frame.origin.y,
image.size.width,
image.size.height);
imageView.frame = newFrame;

Resources