iOS-UIView-設定背景圖片4種方式,iosuiview背景圖片

來源:互聯網
上載者:User

iOS-UIView-設定背景圖片4種方式,iosuiview背景圖片
一 . 設定UIView的背景圖片1.將圖片作為UIView的背景色,該方法過於占記憶體,不建議使用。    //1.imageNamed方式

    self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"image.jpg"]];

     //2.方式       NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];     self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageWithContentsOfFile:path]]; //這兩種方式都會在產生color時佔用大量的記憶體。如果圖片大小不夠,就會平鋪多張圖片,不會去展開圖片以適應View的大小。 //在View釋放後,1中的color不會跟著釋放,而是一直存在記憶體中;2中的color會跟著釋放掉,當然再次產生color時就會再次申請記憶體 2.在UIView上再添加一個UIImageView顯示圖片作為UIView的背景圖片注意:如果有點擊事件的話, userInteractionEnabled使用者互動設定為YES。 3.iOS視圖都是一個圖層,最先放置的圖層就會在最底層,如此最先給UIView添加一個UIImageView就可以作為UIView的背景圖片使用啦。  4.其他方式(推薦) NSString *path = [[NSBundlemainBundle]pathForResource:@"image"ofType:@"jpg"];       UIImage *image = [UIImageimageWithContentsOfFile:path]; self.view.layer.contents = (id)image.CGImage; //注意: 要寫清楚尾碼,即使是”.png”。  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.