iOS - iPad background image is getting cut off short - ios

I have an iPad/iPhone app and the iPhone background looks fine, but when I use the same image for the iPad version, it looks like it is getting cut off in the middle of the page.
Here is a screen shot:
The image is called buildings.png and I have 2 versions of it, the buildings.png and the buildings#2x for the iPad.
The way I get the image to render in the background is from the code like this:
- (void)viewDidAppear:(BOOL)animated
{
UIImage *image = [UIImage imageNamed:#"building"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
What would be the best way for me to fix this issue of the image being cut off like that on the iPad view?
Thanks!

I think the colorWithPatternImage is the reason your image is getting cut like that. Why not create a UIImageView to contain your image, add it on your view and call [self.view sendSubviewToBack:yourImageView] ?
EDIT
Here is your code snippet
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"building"]];
imgView.frame = self.view.bounds; // to set the frame to your view's size
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];

This is what worked for me -
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
imgView.frame = [[UIScreen mainScreen] bounds]; // to set the frame to your view's size
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];

Related

Bug about UIImageView display gif?

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

Add image to view (background)

I have added an image to my viewcontroller in code. Without the use of an imageview. This is how i did it:
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,500)];
backgroundImage.image=[UIImage imageNamed:#"backgroundImage"];
[self.view addSubview:backgroundImage];
There are 2 things i need a litte help with. How can i get the image to the back of the view, behind the labels, buttons etc.
And how do i get the image to fit all iphone screen sizes?
I hope you can help me.
Much thanks
As you asked two questions-
Question 1 : How to get the labels/ buttons other subviews visible over the image?
Answer -
Just add image before any other subview.
And there are two ways to set a background image to a UIView –
You can set your view background color to color created with UIColor’s colorWithPaternImage.
You can add a UIImageView subview to your view.
colorWithPaternImage was created to use small images to create a pattern image that will be repeated. Using it with large images wont be a wise decision. So if you want to have a patterned image background with uses small images which will be repeated to fill the background, then you should use colorWithPaternImage, as it will do it quickly and wont consume much memory.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background"]];
If you have a full size background image, then you should definitely use the UIImageView. Using UIImageView will save a lot of memory in this case.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
[self.view addSubview:backgroundView];
Question 2 : How to show the image as per the device/ screen resolution?
Answer -
CGRect screenBounds = [[UIScreen mainScreen] bounds];
Use the above screenBounds to set the frame of ImageView.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundImage"]];
Just replace your code with this :
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
backgroundImage.image = [UIImage imageNamed:#"backgroundImage"];
[self.view insertView:backgroundImage atIndex:0];
Hope this helps. Let me know it.. :)
Simply get screen size of device by following method to make it compatible to all device.
CGRect rect = [UIScreen mainScreen].bounds;
create UIImageView object of that frame size and insert at index 0 of UIView.
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:rect]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:#"your_image.png"];
[self.view insertSubview:backgroundImage atIndex:0];
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:self.view.bounds]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:#"your_image.png"];
[self.view addSubview:backgroundImage];
// This line will send image behind all the other controls
[backgroundImage sendSubviewToBack:self.view];

Rotated imageview not appearing after addsubview

I have added an arrow image view and I want to rotate it to create a corner.
My code is pretty simple:
[self.view addSubview:self.upLeftCorner];
self.upLeftCorner.transform = CGAffineTransformMakeRotation(2.35619449);
The imageview appears when i don't rotate it, however it does not after the rotation. I tried adding the subview after the rotation but still it does not appear. Any help?
Well, this is working:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, 100, 100)];
imageView.transform = CGAffineTransformMakeRotation(2.35619449);
[self.view addSubview:imageView];
I think its because you did the addSubview before rotating the imageView. In my code, I rotated then addSubview

How to display image in iphone simulator using objective C

I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.

UIImageView shows outside its parents frame

I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;

Resources