2

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?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Simon
  • 172
  • 1
  • 4
  • 18
  • 2
    That's how this is designed to be used. It's designed to create a pattern that fills the entire area so you can turn small tiles into an overall texture. If you're looking for fullscreen background, perhaps using an imageView as your background view is a better choice. – Logan May 13 '14 at 19:32
  • Makes sense with the 'pattern' in the method. Two wonderings: a) no way to 'turn off' repeat/pattern? b) I ran this multiple times with different images, and they were fullscreen – Simon May 13 '14 at 19:38
  • a. No, I'm pretty sure they assume you'll use an imageView if you don't want the pattern. b. It depends on the size of the image. If the size of the image is the size of the screen, it will fill the area. This could change from image to image, and from screen to screen. You'd have to use different images for 3.5 vs 4 inch and a third for the inevitably larger iPhone 6. – Logan May 13 '14 at 19:46

3 Answers3

0

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

JSA986
  • 5,870
  • 9
  • 45
  • 91
0

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)]];
Logan
  • 52,262
  • 20
  • 99
  • 128
  • At first I just saw a lot of code for something I'd hoped could be done in pretty much one line. Now I think I understand most of the logic behind. When I emulate the screen goes black and says 'scene is unreachable due to lack of entry points and does not have an identifier for runtime access. I will try and search around, but for now I don't have much of a clue to what's wrong – Simon May 14 '14 at 17:55
  • I'm not sure I understand what you mean by that? – Logan May 14 '14 at 17:58
  • Have tried using the code above and added an imageView in the storyboard and added this to the header file. Now the screen goes black when I emulate the code – Simon May 14 '14 at 18:02
  • You shouldn't need to add anything to use this. – Logan May 14 '14 at 18:07
  • Of course! should have realized that as the very first line creates an imageView. Haven't gotten past black screen yet though getting 'Multiple build commands for output file', but I have some google hits for that.. so maybe I'll fix that on my own – Simon May 14 '14 at 18:22
  • Yea, that's a strange error, but if you're getting hits, it sounds like others have encountered it and there's probably a solution :) – Logan May 14 '14 at 18:23
  • Well I fixed that solution...BUT When I run without your code involved app runs fine, then when I insert the first part(only in the viewDidLoad), app goes black screen - with 'invalid asset name supplied' – Simon May 14 '14 at 18:50
  • http://stackoverflow.com/questions/22011106/error-cuicatalog-invalid-asset-name-supplied-null-or-invalid-scale-factor this might explain it, I think at least. But haven't had success with that yet – Simon May 14 '14 at 18:58
  • One of many things I don't really get, is that when I search around for 'objective c set background as image' and the like the results are somewhat limited and mostly suggesting the backGroundColor method that I myself used initially. – Simon May 14 '14 at 20:46
  • That seems really odd, you're going to have to maintain 3 times the images or implement a bunch of resizing code for the images if you want to do it that way. – Logan May 14 '14 at 20:50
  • Something else is going on in your code. I have projects that run this just fine. This definitely runs w/o errors. Perhaps something in your imageArray isn't correct – Logan May 14 '14 at 20:51
  • Thanks for all the help Logan! I ended up going back to just using plain color backgrounds, as I never did get it to work and is has taken so many hours. Objective C is tough when you're just crashing in with no real programming experience... – Simon May 16 '14 at 18:36
  • I finally made things work!!! Though a little different, and I just made a thread about it: http://stackoverflow.com/questions/23715404/how-to-set-an-image-background-in-objective-c @logan – Simon May 17 '14 at 19:43
  • Cool, you should put your answer on this thread, and remember, 'x' should be count of array minus 1. Index starts at 0, count starts at 1. Glad this could get you in the right direction. – Logan May 17 '14 at 19:54
0

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)

Simon
  • 172
  • 1
  • 4
  • 18