Using launch image as ViewController background image - iphone

I created my 3 launch images (Default Default#2x Default-568#2x) at their appropriate resolutions. I'd like to use these images as the background image of my initial View Controller.
However, when I designed my launch images, I did it with the statusbar in mind and left that area blank. When I try to set the background image of the View Controller to the launch image using the following code, my launch image begins at the bottom of my status bar and the blank area is visible. Essentially, my 640x1136 launch image is being squeezed into a 640x1096 space. What is the proper way to do what I'm attempting?
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
backgroundImage = [UIImage imageNamed:#"Default-568h#2x"];
}
else{
backgroundImage = [UIImage imageNamed:#"Default"];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
Update:
I replaced:
[backgroundImageView setFrame:[[self view] bounds]];
With:
[backgroundImageView setFrame:CGRectMake(0 ,-20,self.view.frame.size.width , self.view.frame.size.height+20)];
And it seems to be behaving the way I want now.

Try this:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
Also try not setting backgroundImageView's frame programatically. It should already be the size it should be when you call initWithImage:.

Make use of unedited image which does not contain the blank area. this will work fine.
Check replacing this [backgroundImageView setFrame:[[self view] bounds]]; with the following code.
int statusBarHeight = statusBar.frame.size.height;
backgroundImageView.frame = CGRectMake(0 ,statusBarHeight,self.view.frame.size.width , self.view.frame.size.height-statusBarHeight);

Related

set background images for Iphone 3g, Iphone 4s and Iphone 5

I want to set the background image of a view. I use three images: backgroundimage.png (size 320 x 480), backgroundimage#2x.png (size 640 x 960) and backgroundimage-568h#2x.png (size 640 x 1136)
that works for iphone 3g size. But when I test it for retina size the background image is just a small part of the original image. I think, that I have to downscale the image somehow, or set a frame for it, but I just don't know how.
Thanks for any help ;)
thats the code:
self.view.backgroundColor = [UIColor clearColor];
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
backgroundImage = [UIImage imageNamed:#"background-568h#2x"];
}
else{
backgroundImage = [UIImage imageNamed:#"background"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
My first guess is because of the misuse of colorWithPatternImage. This should only be used if you have a pattern image that you wish to have tiled. Instead try creating a UIImageView the size of the view, and then adding it as a subview of self.view.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];

How to set background image for a View?

the default background color is white and I want to change it to an image
I have tried this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
but the background turn black, the image is not at the background. I don't know why.
by the way if I have more than 1 image in one view, how to set their priority of display?
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageone"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
Try this out, this is calling self.view.bounds for the frame instead of self.view.frame for frame. I just tested it and it works perfectly.
EDIT: If all you're looking for is to send the image to the back of the stack, you can do the following and select send to back.
Code for you :
UIImageView *myImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"background.png"]]autorelease];
myImage.frame = self.view.frame;
[self.view addSubview:myImage];
You should to use a UIImageView and its frame is uiview's frame.

uiimageview aspectfit question

I've use a imagepicker to select image from my photo library, then i display that image in an uiimageview. Landscape photo works fine but there is some weirdness to the portrait image. The portrait image suppose to fill up the left and right empty space but it's not.
Cant figure out why the picture wont fill up the left and right space cause the imageview frame did specify the mainScreen bounds.
If i take away the aspectfit then the potrait image is nicely display but the landscape image is stretched to fill up the whole imageview.
My code is as follow:
CGRect frame = [[UIScreen mainScreen] bounds];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame];
selectedImageView.image = image;
selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:selectedImageView];
[selectedImageView release];
scrollView.delegate = self;
[self.view addSubview:scrollView];
[scrollView release];
Here is what it looks like:
Edit: My goal is to display the photo like it's been display in Photos album, start out as fitting in the view and allow zoom in to a certain limit.
Try out
UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo_bg.png"]];
[logoImageView setFrame:CGRectMake(0, 0, 320, [UIImage imageNamed:#"logo_bg.png"].size.height)];
[mainView addSubview:logoImageView];

I would like to have two zoomable UIImageViews...on the same view, but I can't get it to work!

Ok I have been working on this feature for some time now and it is driving me nuts. I have looked all around the web and can't seem to find any answer. I want to have a view that has two UIScrollviews, that themselves contain a UIImageView each. So I can have to images on my view that are SEPARATELY scroll/zoomable.
It seems pretty simple, but I just can't get it to work. I setup the ScrollView/ImageView pairs in IB and hooked them up ok. Now my viewDidLoad code looks like this:
-(void) viewDidLoad
{
// set the image to be displayed, pic your own image here
imageView = [[MyImage alloc] initWithImage: [UIImage imageNamed: #"newBackground.png"]];
// yes we want to allow user interaction
[imageView setUserInteractionEnabled:YES];
// set the instance of our myScrollView to use the main screen
scrollView = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// turn on scrolling
[scrollView setScrollEnabled: YES];
// set the content size to the size of the image
[scrollView setContentSize: imageView.image.size];
// add the instance of our myImageView class to the content view
[scrollView addSubview: imageView];
// flush the item
[imageView release];
// set max zoom to what suits you
[scrollView setMaximumZoomScale:1.0f];
// set min zoom to what suits you
[scrollView setMinimumZoomScale:0.25f];
// set the delegate
[scrollView setDelegate: self];
// scroll a portion of image into view (my image is very big) :)
//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];
// yes to autoresize
scrollView.autoresizesSubviews = YES;
// set the mask
scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// set the view
//self.view =scrollView;
[scrollView setFrame:CGRectMake(0, 0, 320, 240)];
self.view =scrollView;
imageView1 = [[MyImage alloc] initWithImage: [UIImage imageNamed: #"newBackground.png"]];
// yes we want to allow user interaction
[imageView1 setUserInteractionEnabled:YES];
// set the instance of our myScrollView to use the main screen
scrollView1 = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// turn on scrolling
[scrollView1 setScrollEnabled: YES];
// set the content size to the size of the image
[scrollView1 setContentSize: imageView1.image.size];
// add the instance of our myImageView class to the content view
[scrollView1 addSubview: imageView1];
// flush the item
[imageView1 release];
// set max zoom to what suits you
[scrollView1 setMaximumZoomScale:1.0f];
// set min zoom to what suits you
[scrollView1 setMinimumZoomScale:0.25f];
// set the delegate
[scrollView1 setDelegate: self];
// scroll a portion of image into view (my image is very big) :)
//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];
// yes to autoresize
scrollView1.autoresizesSubviews = YES;
// set the mask
scrollView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// set the view
//self.view =scrollView;
[scrollView1 setFrame:CGRectMake(0, 300, 320, 220)];
//I do this only because I don't know what else to do. This effectively adds the scrollview to the scrollview...
self.view =scrollView1;
And the first scrollView loads, but then the second one is nested in that one! I tried doing[self.view addSubView:scrollView1]; but that just made the app crash. I'm trying my best to really dig in and figure out scrollViews as best I can, but I am just getting owned here.
Please help!
Thanks
** To further clarify, all I want is to have a scrollView on the top half of an image that can pan and zoom with an image inside of it, and then another scrollView separate from the firt that can pan and zoom on the bottom half of the screen, independent of the first.**
You need to add both scroll views as subviews of the ViewController's view (self.view):
[self.view addSubview:scrollView];
[self.view addSubview:scrollView1];
You'll then need to manually position those views so they're positioned like you want them (the above code will put the two scroll views on top of each other, with some arbitrary default size which may or may not be what you want).

pagingScrollView Slideshow

I have started trying to understand the way apple implements the photoviewer app for the iPhone after watching both videos from WWDC 2009 and 2010 about scrollViews and paging through photographs and I am taking each step very slowly to understand the processes very well before I implement it all into my app.
I have run into a couple of problems at the moment:
Firstly I have it set up to have a paging scroll view to swipe left and right between photos with a bit of space in between like on the videos but when I add an image inside a UIImageView th image is too big for the screen, I have tried adding UIViewContentModeScaleAspectFit; to no effect.
When I go through the for loop to look at each element of the array containing the images the UIViews overlap and show one photograph on top of the other, I would like to know how to separate them into the next section of the paging scroll view.
- (void)loadView{
UIImage *img0 = [UIImage imageNamed:#"29.png"];
UIImage *img1 = [UIImage imageNamed:#"33.png"];
NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:img0, img2, nil];
CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
pagingScrollViewFrame.origin.x -= 10;
pagingScrollViewFrame.size.width += 20;
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [imgArray count], pagingScrollViewFrame.size.height);
self.view = pagingScrollView;
for (int i=0; i < [imgArray count]; i++) {
UIImageView *page = [[UIImageView alloc] initWithImage: [imgArray objectAtIndex:i]];
page.contentMode = UIViewContentModeScaleAspectFit;
[pagingScrollView addSubview:page];
}}
As I have mentioned I am fairly new to programming for the iPhone and am taking things slowly to fully understand, eventually this program will mimic the native app with pinching and tapping for zoom.
If you're using UIImageViews, you should make sure that your view has its clipsToBounds field set to yes. Try adding:
UIImageView *page = [[UIImageView alloc]
initWithImage:[imgArray objectAtIndex:i]];
[page setContentMode:UIViewContentModeScaleAspectFit];
[page setClipsToBounds:YES];
[pagingScrollView addSubview:page];
Then, make sure you are setting your image's frame to the correct offset within the scroll view. The frame's origin.x needs to be the width of the frame times the image index. So you need something like this:
[page setFrame:CGRectMake(i*pagingScrollViewFrame.size.width, y, width, height)];
where i is your index from your for loop.
Though in the sample code from the WWDC session you're referring to, this is done in a method called -configurePage. Have you downloaded the sample code?
-[UIImageView initWithImage:] sets the frame to be the image's size. You'll need to use page.frame = [[UIScreen mainScreen] bounds] or similar to scale images to the correct size.