A circular image view is being rectangle when make it hidden - ios

I make a circular imageView. Here is the code:
someImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-35,80,80,80)];
[someImageView setContentMode:UIViewContentModeScaleAspectFit];
someImageView.layer.cornerRadius = someImageView.frame.size.height /2;
someImageView.layer.masksToBounds = YES;
someImageView.layer.borderWidth = 1.0;
someImageView.layer.borderColor = [UIColor colorWithRed:16.0f/255.0f
green:56.0f/255.0f
blue:70.0f/255.0f
alpha:1.0f].CGColor;
someImageView.clipsToBounds=YES;
[self.view addSubview:someImageView];
But when I make it hidden, I notice a rect(original size) for a very little time and then hidden. Why this happen? I want to make it circular when I will hidden yes or no.

Related

IOS/Objective-C: Change tint of an image view

I would like to change the color of an image view when the user touches it. I don't need it to change a great deal. However, I would like it to change to a shade of blue. I found the following code to change the gradient but it is not having any effect. I don't need a gradient, actually, only a tint. Would appreciate any suggestions.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor];
[snapshot.layer insertSublayer:gradient atIndex:0];
An easy and non-intrusive way is to add a translucent overlay to your image view. This can then be shown and hidden as the button is pressed and released.
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.bounds];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here
[imageView addSubview:overlay];
}
overlay.hidden = !on;
}
You can try giving the UIImage a tint
UIImage *image = [[UIImage imageNamed:#"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work
imageView.image = newImage;
And then changing that tint when the user touches the image using a tap gesture recognizer

Add image circle in UIImageView using objective c

I have a UIImageView and this is my code to show image :
callRecImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"call_accept.png"]];
[callRecImage setFrame:CGRectMake(133,top, 36, 40)];
[self.view addSubview: callRecImage];
To add circle around image I did this :
callRecImage.layer.cornerRadius = callRecImage.frame.size.width /2; callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor; callRecImage.clipsToBounds = YES;
my image view is :
my output is :
I want to add a circle to my UIImageView, I have followed this link image in circle frame iOS but it's working as I expected
I want a big circle around the phone image. any kind of help will be appreciated.
Thanks in advance
[callRecImage setFrame:CGRectMake(133,top, 36, 40)];
//Set Height and width equal
//Like
[callRecImage setFrame:CGRectMake(133,top, 40, 40)];
use the following code
#import <QuartzCore/QuartzCore.h>
callRecImage.layer.masksToBounds = YES;
callRecImage.layer.cornerRadius = callRecImage.bounds.size.width/2;
callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor;
Add the below line to fill you image in ImageView only
callRecImage.contentMode = UIViewContentModeScaleAspectFill;
Try this code
callRecImage.layer.backgroundColor=[[UIColor clearColor] CGColor];
callRecImage.layer.cornerRadius=20;
callRecImage.layer.borderWidth=2.0;
callRecImage.layer.masksToBounds = YES;
callRecImage.layer.borderColor=[[UIColor grayColor] CGColor]
Whenever you go for layer.cornerRadius for the same view then circle will be always smaller then the view rectangle so use one of these two solution.
I have tested and both the solutions are in working condition
Use following code to change UIViewContentMode of your image and increase your UIImageView frame size (height and width, both should be in equal)
callRecImage.contentMode = UIViewContentModeCenter;
So that if you increase the UIImageView frame size, It will keep your image in center and won't stretch your image inside UIImageView
UIImageView *callRecImage = [[UIImageView alloc] initWithFrame:CGRectMake(133, top, 60, 60)];
callRecImage.contentMode = UIViewContentModeCenter;
[callRecImage setImage:[UIImage imageNamed:#"call_accept.png"]];
callRecImage.layer.cornerRadius = callRecImage.frame.size.width /2;
callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor;
callRecImage.clipsToBounds = YES;
[self.view addSubview: callRecImage];
or
Add one UIView as superview of your UIImageView and add layer.cornerRadius to that UIView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(133, top, 60, 60)];
UIImageView *callRecImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"call_accept.png"]];
[callRecImage setFrame:CGRectMake(view.frame.size.width/2 - 36/2, view.frame.size.height/2 - 40/2, 36, 40)];
view.layer.cornerRadius = view.frame.size.width /2;
view.layer.borderWidth = 2.0f;
view.layer.borderColor = [UIColor grayColor].CGColor;
view.clipsToBounds = YES;
[view addSubview: callRecImage];
[self.view addSubview: view];

How do I (successfully) set a maskView to a UIView?

I'm making a camera-based app and would like to make a frosted overlay on the camera preview, with a rounded rectangle cut out from the frosted view.
I've been trying to achieve this with the maskLayer of UIView, introduced with iOS 8 and have been wildly unsuccessful.
Here's what my code currently looks like:
// Blur the preview
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
blurView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view insertSubview:blurView aboveSubview:imagePicker.view];
// Make blur view fill screen
{
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
}
// Add a rounded of transparency to the blurView **not working**
UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(50.f, 50.f, 50.f, 50.f)];
mask.alpha = 1.0f;
mask.backgroundColor = [UIColor redColor];
[blurView setMaskView:mask]; // Remove this line, and everything is frosted. Keeping this line and nothing is frosted.
As my comments indicate, adding the mask removes the frosted view completely (changing the opacity of the mask does nothing). I know that this won't create the effect I'm looking for (it should only be frosted in that rectangle), but I can't get the maskView to work in any capacity.
The UIView api says
An optional view whose alpha channel is used to mask a view’s
content.
The best way to see this is to use a UIImageView loaded with an RGBA image that sports an alpha channel - a PNG exported with transparency switched on from photoshop will do it. And then resize, with the origin set to 0,0.
A second way to do it would be to create a UIView, with no alpha i.e. [UIColor clearColor] and add a subview which has an alpha [UIColor whiteColor];
UIView *mask = [[UIView alloc] initWithFrame:(CGRect){0,0,100,100}];
mask.backgroundColor = [UIColor clearColor];
UIView *areaToReveal = [[UIView alloc] initWithFrame:(CGRect){20,20,50,50}];
areaToReveal.backgroundColor = [UIColor whiteColor];
[mask addSubview:areaToReveal];
_myView.maskView = mask;
You could add further UIViews which are translucent by x amount using [UIColor colorFromRed: green: blue: alpha:x];

Adding a badge icon to round rect button?

Is there a convenient way to make a button with a badge? Not on the icon... I want to do it within the app. A button or an image with a badge selected by the program. Or do I have to build a library of photoshop images?
You'll need to use resizableImageWithCapInsets to achieve this without a library of photoshop images. There are some great threads (here and here) that explain its use.
Here's an example I just made to give you an idea:
//Create a label (width/height not important at this stage)
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
yourLabel.text = #"7+";
[yourLabel sizeToFit];
CGRect labelFrame = yourLabel.frame;
//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined
UIImage *badgeImage = [[UIImage imageNamed:#"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];
badgeImageView.contentMode = UIViewContentModeScaleToFill;
badgeImageView.backgroundColor = [UIColor clearColor];
labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)
//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size
badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account
//Center the label on the badge image view
yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);
//Finally we add the label to the badge image view
[badgeImageView addSubview:yourLabel];
//Add your badge to the main view
[self.view addSubview:badgeImageView];
[badgeImageView release];
[yourLabel release];
UILabel *lbl_notification_count = [[UILabel alloc]initWithFrame:CGRectMake(5,0, 18, 18)];
lbl_notification_count.textColor = [UIColor whiteColor];
lbl_notification_count.textAlignment = NSTextAlignmentCenter;
lbl_notification_count.text = [NSString stringWithFormat:#"%d",appDel.NotificationBadge];
lbl_notification_count.adjustsFontSizeToFitWidth=YES;
lbl_notification_count.layer.borderWidth = 1;
lbl_notification_count.layer.cornerRadius = lbl_notification_count.layer.frame.size.height/2;
lbl_notification_count.layer.masksToBounds = YES;
lbl_notification_count.layer.borderColor =[[UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0] CGColor];
lbl_notification_count.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:84.0/255.0 blue:67.0/255.0 alpha:1.0];
lbl_notification_count.font = CustomFontMediumWithSize(12);
if (appDel.NotificationBadge >= 1) {
[btnNotification addSubview:lbl_notification_count];
[lbl_notification_count setHidden:NO];
}else{
[lbl_notification_count setHidden:YES];
}

setting an image in background of a label programmatically

I have a label (many labels in fact) now I want an image in the background of my label (same image) so that text writtenin my label is shown to user. How to do it programmatically?
The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.
CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:#"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...
[self.view addSubview:theImageView];
[self.view addSubview:theLabel];
[theImageView release];
[theLabel release];

Resources