iOS: Image is not displaying properly in UIImageView - ios

I have a UIImageView in which a UIImage is showing which comes from JSON. The problem is that I want to set the cornerRadius for the UIImageView. I put the following code for it:
imgVw.layer.cornerRadius = 30.0;
imgVw.layer.borderWidth = 2.0;
imgVw.layer.borderColor = [UIColor blackColor].CGColor;
It displays the cornerRadius and border for UIImageView but not in the proper way. The image is showing outside the corners like in screenshot.

You need to set the masksToBounds property of layer to YES like this
self.imgVw.layer.masksToBounds = YES;

Set the clipsToBounds property of UIImageView to YES.
Like
set imgVw.clipsToBounds=YES;

Related

How to get a circle image in UIImageView

I tried to get a circle image using layer, here is my code
_compassArrow.layer.cornerRadius = _compassArrow.frame.size.width / 2;
_compassArrow.layer.masksToBounds = YES;
_compassArrow.layer.borderColor = [UIColor whiteColor].CGColor
the compassArrow is an imageview which display the compass image. And when I run my program, it looks terrible:
my actual picture
I don't know what happened to it. I've add some constraints to it, to make it has equal width with the device. Does this influence my image?
I think you set cornerRadius before your constraints are applied. Try to put this code in layoutSubviews or viewDidLayoutSubviews for example.
This way, the _compassArrow.frame.size.width value will be the one after constraints applied on it, and you'll get the correct cornerRadius.
Here is a piece of code that should allow you to do this.
_compassArrow.layer.borderWidth = 1.0
_compassArrow.layer.masksToBounds = false
_compassArrow.layer.borderColor = UIColor.whiteColor().CGColor
_compassArrow.layer.cornerRadius = profilePicture.frame.size.width/2
_compassArrow.clipsToBounds = true

How to set transparent background color in UIImageView

I want to know how to set Transparent background Color in iOS. I have searched everywhere but I can't find anything. I want to set transparent background color like this:
Set opaque to NO and alpha to 0.3, like this:
UIImageView *myImageView = ...
myImageView.opaque = NO;
myImageView.alpha = 0.3;
try this one it will works :-
Your_ImageView.opaque = NO;
Your_ImageView.alpha = 0.3;

Adding a layer of color over a UIImage

I was hoping to make my UIImage "highlight" briefly upon being tapped. Not sure of the color yet, but let's say blue for arguments sake. So you tap the image, it briefly looks blue and then it navigates you to a details page to edit something on another screen.
From some initial reading it seems the right course of action is to use the Quartz framework and do this:
imageView.layer.backgroundColor = UIColor.blueColor()
imageView.layer.opacity = 0.7
I guess the idea would be you change the background of the layer behind the image, and then by setting the opacity of the image, the blue "bleeds through" a little bit, giving you a slightly blue image?
When I try the above, however, a blue border goes around the image itself, and based upon the opacity, the blue is either dark or light. The actual image does not become any more blue, but it does react to the opacity (meaning if I set it to something like .1, the image is very faded and barely visible). Why does the image react correctly, but not show blue?
Thanks so much!
As far as I know changing the opacity will change the opacity for the WHOLE view, meaning not just the UIImage that the UIImageView holds. So instead of fading to reveal the UIImageView's background color, instead the opacity of the whole view is just decreased as you're seeing.
Another way you could do it though would be to add an initially transparent UIView on top of your UIImageView and change its opacity instead:
UIView *blueCover = [[UIView alloc] initWithFrame: myImageView.frame];
blueCover.backgroundColor = [UIColor blueColor];
blueCover.layer.opacity = 0.0f;
[self.view addSubview: blueCover];
[UIView animateWithDuration:0.2f animations^{
blueCover.layer.opacity = 0.5f
}];
Here's how I use tint and tint opacities in IOS 9 with Swift -
//apply a color to an image
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {
var image : UIImage
var imageView : UIImageView
image = UIImage(named: "someAsset")!
let size : CGSize = image.size
let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height)
let redCover : UIView = UIView(frame: frame)
redCover.backgroundColor = UIColor.redColor()
redCover.layer.opacity = 0.75
imageView = UIImageView()
imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic)
imageView.addSubview(redCover)
return imageView
}
Is this tap highlight perhaps something you could do with a UIButton? UIButton has all these states off the bat and might be a bit easier to work with, specially if there's something that actually needs to happen after you tap it. Worst case scenario is you have a UIButton that does not trigger any method when tapped.
You can try changing the tint color instead:
UIImage *image = [UIImage imageNamed:#"yourImageAsset"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.tintColor = [UIColor blueColor];
imageView.image = image;

How to set shadow gradient image on UIImageView iOS

Hey I'm new to iPhone and I have been trying to set shadow for UIImageView using shadow gradient image i.e. "Image-Shadow.png" using below code.
imageView.layer.shadowColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:#"Image-Shadow.png"]].CGColor;
imageView.layer.shadowOffset = CGSizeMake(0, 1);
imageView.layer.shadowOpacity = 1;
imageView.clipsToBounds = NO;
imageView.layer.shouldRasterize = YES;
My problem is, i am not getting shadow in my UIImageView using above code. Can you please tell me that is this the correct way to add shadow image in UIImageView or it is possible with some other way?
Problem is with the image. I don't think you can provide a pattern image to create a shadow!
Give a proper color like [UIColor blackColor].CGColor and it should work.
The problem is with this line:
imageView.layer.shadowOffset = CGSizeMake(0, 1);
where you are setting the shadow, but not giving it a visible space to display itself.
Positive or negative values should be given to both parameters.
Positive values stretch "down" and "to the right" of your view, negative values stretch "to the left" and "to the up" of your view.
here is my solution as per my requirement :
//set shadow gradient on image view
UIView *shadowView = [[UIView alloc] initWithFrame:imageView.frame]; //add view behind the image view
shadowView.layer.contents = (id) [UIImage imageNamed:#"Image-Shadow.png"].CGImage;
[imageView addSubview:shadowView];
Thanks to all.

Change border width of label in custom UICollectionCell

I have custom UICollectionViewCell and I have placed 2 UILabels in it. Now I want to change the borderRadius and borderWidth of the labels in it. Can anyone tell me how to do this?
I am doing in this way, but nothing is happening:
self.titleLabel.layer.borderWidth = 2.0;
self.titleLabel.layer.borderColor = [UIColor blackColor].CGColor;
I am putting these two lines in initWithFrame method.
If you want to set the borderWidth of UILabel inside means why you are using
self.titleLabel.layer.borderWidth = 2.0;
Instead of this you can directly use the label name like,
yourLabel1.layer.borderWidth = 2.0;
yourLabel2.layer.borderWidth = 2.0;
Like wise you can change the borderRadius also.
With the following line added the border should appear:
self.titleLabel.layer.masksToBounds = YES;

Resources