IOS-UIView-set the background image in four ways: iosuiview background image
1. Set the background image of UIView1. Use the image as the background color of the UIView. This method occupies too much memory and is not recommended.// 1. imageNamed Mode
Self. view. backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed: @ "image.jpg"];
// 2. method NSString * path = [[NSBundlemainBundle] pathForResource: @ "image" ofType: @ "jpg"]; self. view. backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageWithContentsOfFile: path]; // both methods occupy a large amount of memory during color generation. If the image size is not enough, multiple images will be tiled and the image will not be stretched to fit the View size. // After the View is released, the color in 1 will not be released, but will remain in the memory. The color in 2 will be released. Of course, the memory will be applied again when the color is generated again.
2. Add another UIImageView display image as the background image of the UIView.Note: if there is a click event, set userInteractionEnabled user interaction to YES.
3. the iOS view is a layer, and the first layer is placed at the bottom layer. Therefore, you can add a UIImageView to the UIView as the background image of the UIView..
4. other methods (recommended)NSString * path = [[NSBundlemainBundle] pathForResource: @ "image" ofType: @ "jpg"]; UIImage * image = [uiimagewithcontentsoffile: path]; self. view. layer. contents = (id) image. CGImage; // Note: The suffix must be clear. PNG ".