hi i want to display image in the circle not in the rectangle and all this will display in the tableview
问题:
回答1:
You have to #import <QuartzCore/QuartzCore.h>
and add a cornerRadius to your imageView
[yourImageView.layer setCornerRadius:yourImageView.frame.size.width/2]
maybe you have to add [yourImageView setClipsToBounds:YES]
, but i am not sure about this.
回答2:
One way of doing this is to create a subclass of NSView.
Then in you override drawrect: methods to do your custom drawing. You start by drawing your image in your defined frame. After that, you can either draw over, in the the same frame, an image of a rectangle with a transparent circle in the middle or use NSBezierPath to construct a similar rectangle.
The color of this rectangle, except the transparent circle in the middle, is the same as the background color of the enclosing view.
回答3:
Or you can use -drawRect
method:
- (void)drawRect:(CGRect)rect
{
CGRect aRectangle = CGRectMake(0.0f, 0.0f, 40.0f, 40.0f); //size of image frame
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:aRectangle];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIColor *imageColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];
[imageColor setFill];
[path fill];
}
First of all you need to create your own class inherited from UIView (for example) and put this code into your drawRect
.
回答4:
Create the image as a PNG and add a transparency layer. Select the area around the circle and make it transparent. Then when the image is loaded in the UIImageView, it will appear as just a circle.
For example:
回答5:
Alternative is to use CICircularWrap . It wraps an image around a transparent circle.
For example like this:
then u can use UIImageView's layer setCornerRadius to perfect circular image