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.
Related
I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded borders and so on). Check the image below:
Using Aspect Fill
Using Aspect Fit
So, what I need is to remove the image excess that is out of the frame bounds.
I tried using UIBezierPath, CGImageRef and other methods that I Googled but I'm not finding a solution for this.
In Interface Builder, use the following configuration:
There are two important settings, namely:
Mode: Aspect Fill
Clip Subviews
It can also be done programmatically:
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];
This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.
make a IBOutlet in your controller.
#property (retain)IBOutlet UIImageView* imageView;
and in -(void) viewDidLoad set
imageView.layer.masksToBounds = YES;
In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").
OLD ANSWER:
You can use the contentGravity property of CALayer to make it work
A constant that specifies how the layer's contents are positioned or scaled within its bounds.
#property(copy) NSString *contentsGravity
Use an UIImageView and do
imageView.contentMode = UIViewContentModeScaleAspectFit;
I have a custom view for which I've (in the XIB) created a UIImageView as a subview. How can I make the image view appear in the background? (i.e. so that in the custom view I can create the hands of a clock that will appear over the top)
Do I need to make my background image have alpha areas for transparency? (if I have the terms correct) Or is it OK to have an image with it's background just set to white or black or whatever it needs to be (on the basis it will be in the background so this will be ok)
You can't: views are composited on top of their superviews, which means if your custom view is drawing in -drawRect:, it will always appear under the UIImageView.
The easiest way to achieve the effect you're looking for is to put the image view under your custom view in IB.
Background images generally do not need to have transparency (and there's a slight speedup if the PNG has no alpha channel).
An alternate approach to using UIImageView objects if you are drawing the rest of stuff in drawRect: is to use the image directly. Draw the image in drawRect: before you do any of the drawing.
Example
- (void)drawRect:(CGRect)rect {
[backgroundImage drawInRect:rect];
/* Do rest of your drawing */
}
Now this is only if you are using a complex image. If it is a texture, you would rather use UIColor's colorWithPatternImage: method to get a color from the UIImage object and set your custom view's backgroundColor. Remember that using the colorWithPatternImage: will not scale the image while drawing and will tile if the source image is smaller the frame of the view.
I have one UIView with an UIImageview in which i have an image downloaded from web service. Now i try to draw with drawRect: in it. I want to erase part of my drawing same as like eraser without effecting background image downloaded from web service.
So how can i do that?
Thanks
If you simply want to "replace" part of the UIImageView with rendered primitives, rather than editing the image data, just place a UIView with a [UIColor clearColor] background color on top of the UIImageView and draw into that overlay UIView with a non-transparent color.
I don't think your looking at it the right way.
How I would do this is:
1) get the bitmap in the UIImageView
2) change the pixels/areas that you want to change
3) set the altered bitmap in your view
I have a ball assigned to a UIImageView in Interface Builder. An IBOutlet from the UIImageView is wired to a corresponding UIViewController. The image has a white background. When I assign it to the UIImageView in IB, the background is transparent. In IB, I have the UIImageView set to a transparent background and aspect fill.
When I assign the UIImageView an image at runtime:
self.ball.image = ballImage; //ballImage is a UIImage
self.ball.backgroundColor = [UIColor clearColor];
self.ball.contentMode = UIViewContentModeScaleAspectFill;
the UIImageView square has a white background where the ball doesn't display. Meaning all four corners. What is the difference that the IB version doesn't show a white background at runtime and the programmatic version does?
Make sure you set self.ball.opaque = NO; in addition to setting the background color to clear. Otherwise, a white background will still be drawn. I believe you have to set both of these whether you use IB or Xcode to create the view - but IB may have set them both for you.
If you need to remove background programmatically you may refer to "Bitmap Images and Image Masks" guide. I have no other idea on this point...just using CGImageCreateWithMaskingColors.
In case your *.PNG (or any graphic resource file) doesn't have built-in transparency (just white background) but with IB it looks like transparent - may be alpha property less then 1 for your UIImageView and you see partially transparent image.
set a UIImageView.image property to a UIImage is unable add the image to the view,however if i set the imageview frame property,it works.can anyone tell me how it's happen?
and what will happen when i apply the UIImage instance method[drawInRect:] redraw the image frame which is larger than the view frame?i have tried but nothing happen,and what is this function doing actual?
Generally, how it works is, you add an Image to a UIImageView and then add the UIImageView to a UIView to display it on the screen. You can do this programmatically, or using Interface Builder. Optionally, you can create a CGRect (based on a frame, if you like), use this as the bounds for the UIImageView (or you can frame the UIView). There are several ways to do what you want to do.
It's hard to tell without seeing your code. It could be a number of things. You could be inserting the view with the image behind the currentView? You may not be retaining the UIImageView. We can't read minds here necessarily, (sometimes we do). But you'll need to be more helpful if you want a solid answer.
Or just take a look at this:
UIImageView Docs