Ios: how to display an image as a circular style/set the widget border size and color
For example, the QQ login avatar shows a circle, but in fact its image is not a circle, but drawn by the layer. When it comes to layer, each control has a layer attribute, so any control can be set to a circle, or an elliptical control depends on the project's needs.
UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "header .png"]; imageView. frame = CGRectMake (100,100,100,100); // sets the Corner radius of the control through the layer (each control has a layer, and all other controls can achieve circular effects) imageView. layer. cornerRadius = 50; // At the same time, you must set the excess part to prevent display so that you can reach a circular image view. clipsToBounds = YES; [mainView. view addSubview: imageView];
Let's take a look at what properties the layer has and what effects can be set. One is to set the border size and color of the control, sometimes it is easy for us to test the project control, that is, to clearly display the position of the control. For example, after setting the cell of tableView, we can clearly see the size range of each cell, you can also click to make the control have the selected effect. More advanced, you can use the layer to animation the control.
Below is the apple API
@ Property CGFloat cornerRadius; // set The Corner radius/* the width of the layer's border, inset from The layer bounds. the * border is composited above the layer's content and sublayers and * Events des the effects of the 'cornerradius 'property. ults to * zero. animatable. * // @ property CGFloat borderWidth; // set The border width of the control/* The color of the layer's border. ults to opaque black. colors * created from tiled patterns are supported. animatable. * // @ property CGColorRef borderColor; // set The border color of the control/* The opacity of the layer, as a value between zero and one. defaults * to one. specifying a value outside the [0, 1] range will give undefined * results. animatable. * // @ property float opacity; // specifies the transparency of a control. Some animations use/* When true, and the layer's opacity property is less than one, the * layer is allowed to composite itself as a group separate from its * parent. this gives the correct results when the layer contains * multiple opaque components, but may reduce performance. ** The default value of the property is read from the boolean * UIViewGroupOpacity property in the main bundle's Info. plist. if no * value is found in the Info. plist the default value is YES for * applications linked against the iOS 7 SDK or later and NO for * applications linked against an earlier SDK. */
// You can set up the imageView only in the preceding two sentences. layer. borderWidth = 2; // float type data imageView. layer. borderColor = [UIColor redColor]. CGColor; // The color must be CGColor.