UIImageView中的一些注意點(diǎn)

contentMode屬性

  • 帶有scale單詞的:圖片有可能會(huì)拉伸

    • UIViewContentModeScaleToFill
      • 將圖片拉伸至填充整個(gè)imageView
      • 圖片顯示的尺寸跟imageView的尺寸是一樣的
    • 帶有aspect單詞的:保持圖片原來的寬高比
      • UIViewContentModeScaleAspectFit
        • 保證剛好能看到圖片的全部
      • UIViewContentModeScaleAspectFill
        • 拉伸至圖片的寬度或者高度跟imageView一樣
  • 沒有scale單詞的:圖片絕對(duì)不會(huì)被拉伸,保持圖片的原尺寸

    • UIViewContentModeCenter
    • UIViewContentModeTop
    • UIViewContentModeBottom
    • UIViewContentModeLeft
    • UIViewContentModeRight
    • UIViewContentModeTopLeft
    • UIViewContentModeTopRight
    • UIViewContentModeBottomLeft
    • UIViewContentModeBottomRight

小語法點(diǎn)

  • 不能直接修改:OC對(duì)象的結(jié)構(gòu)體屬性的成員
  • 下面的寫法是錯(cuò)誤的
imageView.frame.size = imageView.image.size;
  • 正確寫法
CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
imageView.frame = tempFrame;

initWithImage:方法

  • 利用這個(gè)方法創(chuàng)建出來的imageView的尺寸和傳入的圖片尺寸一樣

修改frame的3種方式

  • 直接使用CGRectMake函數(shù)
imageView.frame = CGRectMake(100, 100, 200, 200);
  • 利用臨時(shí)結(jié)構(gòu)體變量
CGRect tempFrame = imageView.frame;
tempFrame.origin.x = 100;
tempFrame.origin.y = 100;
tempFrame.size.width = 200;
tempFrame.size.height = 200;
imageView.frame = tempFrame;
  • 使用大括號(hào){}形式
imageView.frame = (CGRect){{100, 100}, {200, 200}};
  • 抽取重復(fù)代碼

    • 將相同代碼放到一個(gè)新的方法中
    • 不用的東西就變成方法的參數(shù)
  • 圖片的加載方式

    • 有緩存
    UIImage *image = [UIImage imageNamed:@"圖片名"];
    
      - 使用場(chǎng)合:圖片比較小、使用頻率較高
      - 建議把需要緩存的圖片直接放到Images.xcassets
    
    • 無緩存
    NSString *file = [[NSBundle mainBundle] pathForResource:@"圖片名" ofType:@"圖片的擴(kuò)展名"];
    UIImage *image = [UIImage imageWithContentsOfFile:@"圖片文件的全路徑"];
    
      - 使用場(chǎng)合:圖片比較大、使用頻率較小
      - 不需要緩存的圖片不能放在Images.xcassets
    
    • 放在Images.xcassets里面的圖片,只能通過圖片名去加載圖片
  • 延遲做一些事情

[abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
// 10s后自動(dòng)調(diào)用abc的stand:方法,并且傳遞@"123"參數(shù)
  • 音頻文件的簡(jiǎn)單播放
// 創(chuàng)建一個(gè)音頻文件的URL(URL就是文件路徑對(duì)象)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音頻文件名" withExtension:@"音頻文件的擴(kuò)展名"];
// 創(chuàng)建播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容