UIView background image not rotating - objective-c

I've got an app that has two different background images. The one selected is determined by the orientation. When I start out, I check self.interfaceOrientation, and then go and pick the proper image. However, whenever the view opens, part of the image repeats instead of stretching. I saw a previous answer applying autoresizing masks to an imageview, but there is no imageview that I'm currently using.
Inside the loadView method:
if(self.interfaceOrientation==UIInterfaceOrientationPortrait ||self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"portrait"]]];
}else{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"landscape"]]];
}

As rishi had pointed out, the issue was caused by the colorWithPatternImage method. What I did to resolve this was to set the background of the view to be a specified image.
UIImageView* bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed: #"foo.png"]];
[self.view addSubview: bgView];
I also added in flexible width and height so that it would rotate properly.

Related

Can't get a screenshot of UIView to be displayed inside UIImageView

Here's my code to get the screenshot of the view:
if (NULL != UIGraphicsBeginImageContextWithOptions){
UIGraphicsBeginImageContextWithOptions(pagedScrollView.frame.size, NO, [[UIScreen mainScreen] scale]);
}else{
UIGraphicsBeginImageContext(pagedScrollView.frame.size);
}
[pagedScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:screenshotImage];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];
I'm 100% sure I have the right frame and the right view - the size of UIImageView say about it - but the thing is I can't get my UIImage to be displayed on the UIImageView.
The UIImageView is added on the view and I see a green region on it - but no image inside it, what can be wrong?
I'm using iOS 6 and ARC.
Well, I just tested your code running on my view controller's view and the capture and display worked fine. This leads me to believe that pagedScrollView is probably nil, or has a zero frame.
Either way, you'll be creating an image from a blank context and then passing it to your image view resulting in there not being any visible image.
It turns out I've been taking a screenshot of a long UIScrollView and I was always taking its first page - it had a lot of pages in it and sometimes the layer had nothing in it. It also turns out that UIImage was never released and the memory can effectively store dozens of them.

Image aspect ratio change after inserting it into imageview

So I'm trying to load an image from camera into image view. I've got it done, but images aspect ratio changes.
I tried setting content mode for imageview but no effect.
Here is how i do it:
UIImage *picture=[UIImage imageWithData:slika];
imageview = UIViewContentModeScaleAspectFit;
[imageview setImage:picture forState:UIControlStateNormal];
Any ideas?
Is image view your own view? Or it's, for example, cell's default imageView?
And you should write so:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:picture];
or
[imageView setImage:picture];
There is no method [imageview setImage:picture forState:UIControlStateNormal]; for UIImageView, it only available for UIButton (and may be for some other UIControls, but UIImageView not subclass of UIControl - it direct subclass of UIView) . But button does not scale images preserving aspect ratio.

Centre a view in its parent, when in landscape

I've search and found a few questions about this, but none containing an answer that worked for me.
This code centres a subview in the current view, if the device is in portrait, but not if it's landscape. How do I make the centring work in landscape?
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
redView.backgroundColor = [UIColor redColor];
redView.center = [self.view convertPoint:self.view.center fromView:self.view.superview];
[self.view addSubview:redView];
Rather try using
redView.center = [self.view convertPoint:self.view.center fromView:nil];
Works for me.
Note from the docs:
The view with point in its coordinate system. If view is nil, this
method instead converts from window base coordinates. Otherwise, both
view and the receiver must belong to the same UIWindow object
(should be called in viewDidAppear, not in viewDidLoad)

iOS: Background image in UIImagePickerController is inconsistent with the rest of the app

I'm using the following code to set the navbar's background:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"tile.png"] forBarMetrics:UIBarMetricsDefault];
This results in a nice tiled background such as you see here:
However, when I open up a UIImagePickerController, the background is warped somehow and we end up with something like this:
Anyone have suggestions on how to fix it?
Stretching instead of tiling: to Stop That
Create a class of type UINavigationBar
Create your custom UINavigationBar subclass.
Comment initwithFrameMethod
Add method DrawRect:
(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"bluebackground.jpeg"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
4.in AppDelegate.m, AppliocationdidFinishLaunching
Write
[self.navigationbar setValue:[[customBar alloc]init] forKeyPath:#"navigationBar"];
Adding Image By this way will not stretch the image
Refer this Link:
http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
http://designm.ag/tutorials/designing-a-custom-iphone-app-navigation-bar/
Hope this will help you.

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.