Layers of Images using UIImageView - iphone

I just wanted to confirm, is it possible that we add layers of images using UImageView, one as background and other on top of it. I mean i want to use 2 images one has to be in the background and the other on top of it. ( the size of both these images is 320 x 480 ).
Through the attributes inspector we cant do it. Is there any we can do it through code.
Maybe using subview or something.

You need one UIImageView for each image, but they will appear layered as long as the top view is not opaque. You could put both image views into a third UIView if you want to treat them as a single entity.
Edit:
imageBackground = [[UIImageView alloc] initWithImage:[UIImageNamed:#"background.png"]];
imageForeground = [[UIImageView alloc] initWithImage:[UIImageNamed:#"foreground.png"]];
imageLayered = [[UIView alloc] initWithFrame:[imageBackground frame];
[imageLayered addSubview:imageBackground];
[imageLayered addSubview:imageForeground];
// release all views at some point.
imageLayered is now a UIView with a background and foreground image.

Related

is it safe to add UIView to another so that child view's frame is much larger than parent's frame

I want to add a UIView to another UIView so that child view covers all of its parent.
Initially I did:
UIView * childVU;
childVU = [[UIView alloc] initWithFrame:parentVU.frame];
When I added childVU to parentVU, childVU's top was lower than that of parentVU.
so I did:
childVU = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10000.0, 10000.0)];
It is running correctly on simulators, but is it really safe to do that?
You where on the right track with your first code.
Except you should have used bounds and not frame, since the frame is relative to the parent.
UIView * childVU;
childVU = [[UIView alloc] initWithFrame:parentVU.bounds];
Although making a child view large will work, it not the correct way to do it, but if clipsToBounds set to YES on the view or masksToBounds is set to YES it will nog draw child views larger then it own size.
Yes that's fine. Ensure your parent view has clipToBounds disabled prior to displaying the content. Also that is a massive view size and it will be unsupported on older devices. If you are actually using content this size you should place it in a UIScrollView or a CATiledLayer.

UIImageView not showing the background View if the image has transparent regions

I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.

Setting a jpg as the background for a UIView?

I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when the iphone is rotated.
-thanks
You need to use a UIImageView, which is a subclass of UIView. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.

UINavigationBar with a static image

Adding an image to a UINavigationItem's titleView is pretty trivial, but when I push a new view on the stack it animates the UINavigationItem sliding off to the left, along with the titleView, and loads a new UINavigationItem.
I can think of a number of ways to go about making that image stay put, but they all seem pretty hacky. Is there a normal way of doing this that I can't find? Following is code for adding an image to my view controller's UINavigationItem:
UIImage *tImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"logo_topbar" ofType:#"png"]];
UIImageView *tImageView = [[UIImageView alloc] initWithImage:tImage];
self.navigationItem.titleView = tImageView;
[tImage release];
[tImageView release];
Thanks guys/gals
I imagine you could embed the entire view into a super-view, with the static image underneath your existing UI. You can then make the header in the UINavigationItems transparent, so the buttons will slide around over the image, but the image will stay put.
You can add the same titleView to all UIViewControllers that you would like it to appear on, I would guess however that this would let the ImageView be animated out to the left and a new identical one coming in from the right.
The only way I see is to add the UIImageView like this:
[self.navigationController.view addSubview:tImageView];
or try this
[self.navigationController.navigationBar addSubview:tImageView];

Combining images

I want to place the picked image on top of another image, so that my picked image will be placed in some sort of frame I made in Photoshop. After combining the images I want to save it to the disk.
Does anyone know how to do it, or maybe have a link to examples?
You can just layer the two images on top of each other, first add the frame image, then add the image with the photo...
Heres sample code
Assuming your 2 images are already sized correctly to fit one on top of t he other, this code would be in a view controller
UIImageView *frame=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Frame.png"]];
UIImageView *pic=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
frame.center=[self.view center];
pic.center=[self.view center];
[self.view addSubview:frame];
[self.view addSubview:pic];
here it is, memory managment has not been written in..
You can also add the picture UIImageView directly the the frame UIImageView: same as Daniel's suggestion above, but [frame.view addSubview: pic] instead.