colorWithPatternImage vs CGImage vs UIImageView - ios

I've seen on many forums that you shouldn't use colorWithPatternImage for memory issues, you should use UIImageView if you are using a large background image and add as a subview to save memory. I tried with these three solutions and all of those are showing same memory used:
// First Option
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Background"]];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
//Second Option
UIImage *image = [UIImage imageNamed:#"Background"];
self.view.layer.contents = (id) image.CGImage;
//Third Option
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background"]];
I am using iOS7 SDK. Is there anything I am missing or have iOS7 improved in this direction?

Related

iOS7 custom loading screen. PNG sequence, GIF, or animated UIimages

In our iOS app, we want a loading image which is just our logo, animated. I'm wondering what the best practice is to do this? Would it be to use a series of .png images, or one long .png image. I can also thing of using a .gif but the colour quality is not the greatest.
It may also be possible, since the animation is just resizing of certain elements, to do the animation programatically using several UIimages and resizing them, though it may not be as smooth.
UIImage* img1 = [UIImage imageNamed:#"image_1.png"];
UIImage* img2 = [UIImage imageNamed:#"image_2.png"];
UIImage* img3 = [UIImage imageNamed:#"image_3.png"];
UIImage* img4 = [UIImage imageNamed:#"image_4.png"];
UIImage* img5 = [UIImage imageNamed:#"image_5.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4,img5,nil];
UIImageView* imageView = [[UIImageView alloc]
initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 50, 80.0, 80.0)];
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:100];
[imageView setAnimationDuration:3.0];
[imageView startAnimating];
imageView.tag = 100;
[self.view addSubview:imageView];

How to make immovable background in iOS?

In my application, I have UITableViewController, with table on it.
I filling background with image in this way:
UIImage* backgroundImage;
backgroundImage = [UIImage imageNamed:#"bg.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
But, background moves with all other view controller components, while user scroll the table.
Is it possible, to make immovable background, like HTML background?
Thanks.
Try
UIColor *color= [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Background.png"]] autorelease];
self.tableView.backgroundColor = color;
or
UIImageView *imageView= [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Background.png"]] autorelease];
imageView.frame = self.tableView.frame
self.tableView.backgroundView = imageView;

iOS - iPad background image is getting cut off short

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];

adding a background image turns the background all black - likely image not detected

I created a group called img and put an image there that I wanted to appear as the background.
But instead, the background turned all black.
Here is the code I used for this. I put the code in the viewDidAppear method
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"img/building.png"]];
any thought on what I might have done wrong?
Use [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"building.png"]]; instead
I am assuming you are loading an image from the application bundle? If this is the case, the UIImage is not being created correctly.
Try:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"building" ofType:#"png"]];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Repeating background image for UITextView?

Currently I am doing this to have a background image for my UITextView:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
imgView.image = [UIImage imageNamed:#"background.png"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[imgView release];
But the problem is, it does not repeat. What I want, is the image to repeat after each UIImage.
Is this possible?
Thanks!
textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed#"background"]];
Yes, omit the .png from the image name, it will allow you to provide a Retina version background#2x.png.

Resources