How to add image as in circular frame in ios [duplicate] - ios

This question already has answers here:
image in circle frame iOS [closed]
(2 answers)
Closed 6 years ago.
In my app i want show image of the caller in circular frame. How to change image view from rectangular frame to a circular one. It should also fit the caller image!

You can use corner radius property like,
self.imageViewProfile.layer.cornerRadius = 26.5;
self.imageViewProfile.layer.masksToBounds = YES;
Corner radius should be half of the height and width of imageview & height and width should be equal of imageview (i.e. height = width).
By this setup you will able to get rounded imageView.
and maskToBounds set your image as per shape of rounded imageView
Hope this will help :)

Image should be equal height and equal width than you can create circular image.
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.clipToBounds = true;
I think it would be help full for you.If you have any issue plz let me know.

Please try this code it works for you
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.borderWidth = 0;

Related

UIImage in self sizing UITableViewCell stretching

So I have a tableview setup so it displays some information text with an image, the text can vary so I've set up the constraints that the cell updates its size by itself, however the images are stretching as shown below and i've checked that the content mode is set to Aspect Fit.
As it can be observed from the image there are 3 Views in the cell:
-The Title
-The Image, which is being stretched
-The description
My constraints are set up as followed:
Title.top = topMargin
Title.trailing = trailingMargin
Title.leading = leadingMargin
Title.height = 32
Image.leading = leadingMargin
Image.top = Title.bottom + 16
bottom.margin = Image.bottom
Image.width = SuperView(content of cell).width * 0.4
text.leading = image.trailing + 16
trailingMargin = text.trailing
bottomMargin = text.bottom
text.top = topMargin + 16
How can I fix this issue so the images don't stretch? If any additional information is needed please ask and i'll add it.
Thanks
-Jorge
Add a height constraint to the image matching its width so that it maintains its square aspect ratio. Or any height for that matter. Just fix the height so that it does not stretch.
image.height = SuperView(content of cell).width * 0.4
For an app I just created, the tableViewCells and images exhibited something similar, stretching after the cell was scrolled off screen and back into view.
Assuming the image is retrieved from a server (or a file with image data), create a UIImage with a scale factor before setting it as the image of the UIImageView in the tableViewCell. I experimented with a few scale factors (instead of calculating, so your results may vary) before setting it as 5... it is better to calculate for production code but in prototyping, this heuristic worked:
UIImage* scaledImage = [[UIImage alloc] initWithData:data scale:5.0f];
// ... On MainThread
[cell.imageView setImage:scaledImage];

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

bounds of imageView are always 240

I have a probably simple mistake that drives me crazy.
I'm working with UIImageView within a UIScrollView. To fit the image in the view I want to get the width of the imageView to adjust the zoom scale.
But the code
imageView.bounds.width
always returns 240.0 no matter what size the actual image has.
In the Interface Builder the imageView is horizontally and verically centered in the view, clip subviews is true and Mode is aspect fit.
Any ideas?
The size of the UIImageView is not related to the size of the image it contains. The UIImageView is probably sized to 240.0 in the storyboard or wherever else you generate it. The image will scale down or up to fit the view based on the mode. To get the size of the actual image, try the following code:
let image = UIImage("my_image_file")
let imageHeight = image.size.height
let imageWidth = image.size.width
With the size of the image now know, you can set the size of the view appropriately.
I had the same problem. Now I check the bounds in the main_queue and everything works fine.
dispatch_async(dispatch_get_main_queue(), {
print(self.image.bounds.width)
})

Unexpected NSLayoutConstraint behaviour in IOS7

I am getting this issue, only in iOS versions prior to 8
The selected view in the very first screen shot is a UIImageView. you can also see the constraints i have connected. I need to make the image view circular, so i am using layer.cornerRadius property. normally for circular UIImageView we use cornerRadius = height/2.0
but when i am setting this property in "viewDidLayoutSubviews" method. some how its giving me a square imageview.
as you can see i have set aspect ratio of image 1:1
so it can be turned into circular UIImageView.
I know I must be missing out some facts about NSLayoutConstraints. please help me out
My Code :-
-(void)viewDidLayoutSubviews
{
float height = profile_InfoSubView_ProfilePic.bounds.size.height;
float width = profile_InfoSubView_ProfilePic.bounds.size.width;
NSLog(#"(w,h)= %0.0f , %0.0f",width,height);
float cr = height/2.0;
profile_InfoSubView_ProfilePic.layer.cornerRadius = cr;
profile_InfoSubView_ProfilePic.layer.masksToBounds = YES;
profile_InfoSubView_ProfilePic.layer.borderWidth = 2.0f;
profile_InfoSubView_ProfilePic.layer.borderColor = [UIColor greenColor].CGColor;
}
Console Output
2015-09-08 10:42:35.156 MyApp[821:60b] (w,h)= 240 , 128
as you can see even after setting image aspect ration to 1:1, its not square
Output On Device (iPhone 4 - iOS 7.1 )

UIImageView with fixed width: how to set its height to keep its aspect?

I have a view where I need to place an UIImageView, and I was told to place it inside a rectangle that takes the screen width and its height is smaller than width (fixed size). However, the images I've been given are more or less square jpeg images, so the idea is to fill the width of the rectangle that should contain the image and set the height of the image in a way that its aspect ratio is kept. Then, I should be able to let the user scroll the image vertically to see the complete image.
How could I do this?
Thanks in advance
EDIT: I need to do this for several images that have different sizes, but that should fit the same rectangular area size within the view
You can set imageview content mode.
imageView.contentMode = UIViewContentModeScaleAspectFit;
This will make sure that the image is displayed by keeping original aspect ratio.
Edit:
I think this is what you wanted:
UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
Hope this helps.. :)

Resources