This question already has answers here:
How to set background image of a view?
(7 answers)
Closed 8 years ago.
I have set my background to be an image with
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[self.Images objectAtIndex:arc4random_uniform(4) ]]];
This worked instantly, and in effect the background changed between two images... until I changed something in the array that holds the images. Then suddenly the images are repeated in tiles all over the background.
What could cause something like this?
colorWithPatternImage: Is the key here, your using your image to create a pattern, not as a background. As logan has explained you need to re think how your setting your background. If you want to set your background from a random image maybe post a new question on how to do this
Here's something that should solve your problem, you can put it in viewDidLoad:
UIImageView * bgImageView = [[UIImageView alloc]init];
bgImageView.frame = self.view.bounds;
bgImageView.contentMode = UIViewContentModeScaleAspectFit;
bgImageView.image = [UIImage imageNamed:[self.Images objectAtIndex:arc4random_uniform(4)]];
self.view = bgImageView;
You can change/access it later by doing this:
UIImageView *bgImageView = (UIImageView *)self.view;
bgImageView.image = [UIImage imageNamed:[self.Images objectAtIndex:arc4random_uniform(4)]];
I ended up with the following solution:
1) Created a UIImageView in the interface builder AND dragged this out to fill the entire screen
2) Set the content of the imageView with this command:
imageView.image = [UIImage imageNamed:#"Themes.png"];
or if using an array:
imageView.image = [UIImage imageNamed:self.myArray[x]];
Where "x" is of course the number of the element in the array -1 (0 indexing)
Related
Im currently stylizing my login screen of my app, but I am running into a problem that i can't solve and is very odd... The image is not resizing properly and kind of starts to repeat itself, which i find very weird because i have the correct image sizes and have them correctly named, and i am calling them in my code correctly too, so i cannot find my problem. Below is the code.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sl_login_default"]];
colorWithPattern is meant to create a pattern out of an image. If you want to have an image in the background, create a new UIImageView
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName];
backgroundImageView.frame = self.view.bounds;
[self.view addSubview:backgroundImageView];
This question already has answers here:
How can I produce an effect similar to the iOS 7 blur view?
(12 answers)
Closed 8 years ago.
I have a UIView that partially cover some contents and I would like apply a blur effect exactly like the iOS Notification Center.
I'm not looking for a way to apply blur on UIImage, but on UIView with other contents (UIView, UIButton, ecc) under it.
Ideas ?
Thanks.
First change the UIView into an UIImage.
You can use the following as a category of UIView.
#implementation UIView (ConvertToUIImage)
- (UIImage *)asImage {
NSAssert(self, #"Do not use this method on nil instance of UIView.");
if (!self) return 0;
UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 0);
[self drawViewHierarchyInRect:self.frame afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSAssert(image, #"Image must not be nil at this point.");
return image;
}
#end
Next you need to blur the UIImage, there is a related question here Creating a blur effect in iOS7
Finally, use the UIImage as a background.
myView.backgroundColor = [UIColor clearColor];
UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:myView.frame];
bgToolbar.barStyle = UIBarStyleDefault;
[myView.superview insertSubview:bgToolbar belowSubview:myView];
just past from an other answer how ever i use toolbar in my projects and works perfect, i am using alpha=0.98
I would like to fill up an empty UIImageView by another Image.However the two pictures have not the same width, Height and shapes.So what I should do is to scale the second image until it fills up all the area and it should be displayed only there.My problem is how should I proceed.For this moment I can only put the image into the empty area without filling it up.Any idea please ?
please set clipsToBound=YES
UIImageView *img = [[UIImageView alloc]init];
img.image = [UIImage imageNamed:#"yourimage"];
img.contentMode = UIViewContentModeScaleAspectFill;
img.clipsToBounds = YES;
Simply set the content mode to UIViewContentModeScaleAspectFill, as H2CO3 suggested. Try this.
yourImage.contentMode = UIViewContentModeScaleAspectFill;
img.contentMode=UIViewContentModeScaleAspectFill;
This can solve your problem.
Can anyone help me understand how I apply a background image object to a UIView please?
I have created a background image which is a blurred version of a background and I would like to apply it to be the background of a uiView in the foreground which would ideally mask the background image.
I have the following code so far -
_blurImage = [source stackBlur:50];
[_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:[_blurImage]]];
I would like to apply the image object(_blurImage) to be the background image of _hpBlurView but i'm struggling to get it working!
At first glance, you are using too many brackets. Here is a working version of your code :
_burImage = [source stackBlur:50];
_HPBlurImage.backgroundColor = [UIColor colorWithPatternImage:_blurImage];
I can't see what stackBlur:50 returns. So start from the beginning. colorWithPatternImag takes UIImage as a parameter. So Start by adding a picture, any picture, to your application. Lets imagine that the image is called image.png. This is one way to do it:
UIImage *image = [UIImage imageNamed:#"image.png"];
_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:image];
This should help to get you going.
Create an image and add to the background.:
UIImage *image = [UIImage imageNamed:#"youimage"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
It's that.
To make sure everything resizes properly, no matter rotation, device size and iOS version, I just set an UIImageView
//Create UIImageView
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.frame]; //or in your case you should use your _blurView
backgroundImageView.image = [UIImage imageNamed:#"image.png"];
//set it as a subview
[self.view addSubview:backgoundImageView]; //in your case, again, use _blurView
//just in case
[self.view sendSubviewToBack:backgroundImageView];
I couldn't find anything on how you can center the 'image' when you use (maybe you can't):
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background"]]];
Setting my [self setContentMode:UIContentModeCenter]; doesnt help.
Do I have to manually draw the image (either in drawRect or set the content of the CALayer? Which one if preferable?
I think you're on the wrong path here: you create a UIColor from a pattern (pattern already implies this is a repeating image). All in all- you can't have your pattern not repeat and centered.
If you just want simple image as background of your UIView, just add it as a subview and center it.
UIImage* img = [UIImage imageNamed:#"yourfile.png"];
UIImageView* imgView = [[UIImageView alloc]initWithImage: img];
[yourUIView addSubview: imgView];
imgView.center = CGPointMake(yourUIView.frame.size.width/2, yourUIView.frame.size.height/2);
Now - add more subviews to your "yourUIView" view and they'll show on top of the image - thus the image becoming the background.
So... no need to draw anything yourself.