IOS-UIView-set the background image in four ways: iosuiview background image

Source: Internet
Author: User

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 ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.