Overlaying UIView with an background image - ios

Is it possible to overlay UIView with an image without using UIImageView from the Interface Builder?
If not, how would you accomplish the same in code?

From the UIView documentation:
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.
Using the second option, to set someView to have a background of yourImage.png:
someView.layer.contents = (id)[[UIImage imageNamed:#"yourImage.png"] CGImage];

if by Overlying a UIView you mean painting its background with an image, it can be done like this:
someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]];
You could also add a new UIImageView that covers your UIView
by code, Init your UIImageView with the same frame Rect as your UIView and add it as a subview.

Related

Get uiview section under uiimageview?

I have an UIView as background, and a UIImageView above it.
What I want to do is fill the UIImageView with the UIView section that is in the back (without the white border)
I tried cropping a snapshot of the background but it doesnt look good. there is always a difference.
Make the background UIView a subview of the UIImageView and then set the property of the UIImageView yourImageView.clipsToBounds = YES
Use CALayer mask. The mask will be the smaller image view, and will be assigned to the background view's mask property.

UIImageView subclass for circle icon in IB

I need to create a UIImageView subclass that cuts an image to a circle shape, and draws a border.
I set the image view and image in a storyboard.
The initWithCoder method of my subclass is called, but other methods, such as drawRect: and setImage: are not called (I set breakpoints in every method). And the image that appears is not cropped.
Why are drawRect: and setImage: are not called?
Why you don't use imageView.layer?
for example:
imageView.layer.cornerRadius = imageView.frame.size.height/2.0f; // for circle shape
imageView.layer.borderWidth = 3.0;
imageView.layer.borderColor;
imageView.clipsToBounds = YES;
P.S. I'm sorry that i write here, small reputation...

How to avoid tiling when setting a view's backgroundColor?

I'm trying to set up a background image for my app, but the image is tiling. How do I prevent this? I want my image to fit to the background.
In CSS I would set NO-REPEAT; is there something like that?
I have tried this code:
-(IBAction)btnAddBackground
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"image.jpg"]];
}
The documentation for UIView makes a couple of suggestions for this:
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.

iOS - how do I only update my UIImage on the UIView

I've got an UIImage on a UIView. Instead of redrawing the entire UIView, I just want to redraw that specific UIImage. How do I do that?
UIImage on a UIView is not possible. I Think you mean a UIImageView on a UIView.
You can send to the UIImageView...
[yourImageView setNeedsDisplay];

Blacking out areas of a UIImageView or showing part of the view

Is there a way to black out a region (defined by a frame) of a UIImageView without creating an overlaying UIView instance (with this frame) and blacking that out?
Is there similarly a way to reveal part of a UIImageView without using UIView instances to black out the rest of the image?
I am going to cheat a bit here and suggest that you use layer as they are light weight.
CALayer *blackLayer = [CALayer layer];
blackLayer.backgroundColor = [UIColor blackColor];
blackLayer.frame = imageView.bounds;
[imageView.layer addSublayer:blackLayer];
For the second part you can consider using a grid of layers (black). When user touches the image view, you can pick the layers from the area he has touched and remove them from the super layer i.e. the image view's root layer.
you can subclass the UIIMageView and draw as per your needs in the drawRectMethod.
You can create a subclass of UIIMageView and have your definition of drawRectMethod over there.
You can also invoke the drawRectMethod at your will by calling setNeedsLayout method on the view .( setNeedsDisplay - causes drawRect to be called - . Only contentMode = UIViewContentModeRedraw will cause setNeedsDisplay to be called when a view's bounds changes).
UPDATE:
http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html

Resources