UIImageView的運用

UIImageView的運用

本節知識點小結:

  1. UIImageView-設置圖片和內容模式
  2. UIImageView-設置imageView的frame
  3. UIImageView-資源管理
  4. UIImageView-幀動畫的基本使用
  5. UIImageView-播放音頻文件
  6. UIImageView-加載無緩存的圖片
  7. UIImageView-使用總結

1. UIImageView-設置圖片和內容模式

  • 分析一個新聞首頁哪些是圖片控件
  • storyboard創建

    • 找圖片 <圖標引擎>
    • 把圖片放在項目中(以前<Supporting Files> 現在<Images.xcassets>)
    • 兩者的區別, 後者帶後綴 蘋果建議採用後者
    • 設置UIImageView的模式
  • 代碼創建一般使用步驟:

    1. 使用UIImageView,創建imageView;
    2. 設置UIImageView 創建出來的imageView 的位置、尺寸;
    3. 設置UIImageView 創建出來的imageView 的背景顏色;
    4. 將UIImageView 創建出來的imageView 的加入到控制器view 中;
    5. 設置顯示的圖片;
    6. 內容模式 contentMode(一般用來控制圖片如何顯示)
    7. 超出imageView邊框的內容是否需要裁剪掉
    • 注意:6. 內容模式
    1. 帶Scale:顯示的圖片可能會被拉伸:
     UIViewContentModeScaleToFill,
    2. 帶有scale單詞的,並且帶有aspect單詞的:可能會被拉伸,但是會保持圖片原來的寬高比
     UIViewContentModeScaleAspectFit, // 保證剛好能看到圖片的全部
     UIViewContentModeScaleAspectFill, // 拉伸至圖片的寬度或者高度跟imageView一樣
    3. 不帶Scale:顯示的圖片一定不會被拉伸,保持圖片原來的寬度和高度
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
  • 例子:

    - (void)test{
    
      // 1. 創建UIImageView,
      UIImageView * imageView = [[UIImageView alloc]init];
      // 2. 設置UIImageView 創建出來的imageView 的位置、尺寸
      imageView.frame = CGRectMake(100, 200, 50, 100);
      // 3. 設置UIImageView 創建出來的imageView 的背景顏色;
      imageView.backgroundColor = [UIColor greenColor];
      // 4. 將UIImageView 創建出來的imageView 的加入到控制器view 中
      [self.view addSubview:imageView];
    
      // 5. 設置顯示的圖片
      imageView.image = [UIImage imageNamed:@"2"];
      // 6. 內容模式(一般用來控制圖片如何顯示)
      imageView.contentMode = UIViewContentModeLeft;
      // 7. 超出imageView邊框的內容不會被裁剪掉
      imageView.clipsToBounds = NO;
    }

2. UIImageView-設置imageView的frame

  • initWithImage :
    默認尺寸就是圖片的尺寸,位置默認從(0,0)開始

  • 修改frame三種方法

    • 方法一:通過獲取圖片的尺寸信息,並給結構體屬性賦值(常見寫法)

      // 注意:要先獲取到圖片才能使用圖片的尺寸信息
      imageView.image = [UIImage imageNamed:@"2"];
      imageView.frame = CGRectMake(100, 200, imageView.image.size.width, imageView.image.size.height);
    • 方法二:使用CGRectMake(x, y, width, height),直接給frame 賦值(常見寫法)

    imageView.frame = CGRectMake(100, 200, 100, 200);
    • 方法三:定義一個結構體變量,設置該變量的值,在將該結構體變量賦值個frame(神奇三部曲
    // 注意:OC語法規定,不能直接修改OC 對象結構體屬性的成員
    //    imageView.frame.size.width = 200;// Xcode報錯
    //    imageView.frame .size.height = 100;// xcod報錯
    //    imageView.frame.size = CGSize {200,100};// Xcode報錯
    //1. 定義結構體變量,並獲取原有的frame 信息
    CGRect tempFrame = imageView.frame;
    //2. 給結構體每個成員賦值
    tempFrame.size.width = 200;
    tempFrame.size.height = 100;
    tempFrame.origin.x = 100;
    tempFrame.origin.y =200;
    //3. 給frame 賦值回去
    imageView.frame = tempFrame;
    • 注意:修改frame的方法三中方式(同樣適用於bounds/center)
      1.直接使用CGRectMake函數
      2.利用臨時結構體變量
      3.直接運用結構體賦值

3. UIImageView-資源管理

  • 圖片放到supporting Files

    • 是否勾選copy

      1. 勾選,將文件拷貝放在項目的文件夾中,文件獨立,與源文件不會相互影響
      2. 不勾選,項目文件夾中是沒有的,僅僅是引用,與源文件有直接的聯繫
    • Add to targets

      1. 勾選,把文件資源打包到軟件安裝包中去
      2. 不勾選,程序打包後,安裝包中沒有這個資源
    • Added folder

      1. 如果勾選Create groups,會創建一個虛擬的文件夾,程序打包後,安裝包中不存在這個文件夾;
      2. 如果勾選Create folder references,真的創建一個文件夾(文件夾顏色不一樣),程序打包後,安裝包中真的有這個文件夾.
    • 注意事項:

    • 默認無緩存,代碼塊或者圖片文件使用結束時,就會釋放圖片所佔內存
    • 在代碼中訪問獲取圖片的時候需要分開寫後綴名;
    imageView.image = [UIImage imageNamed:@"2.jpg"];
  • 圖片放到images.xcassets

    • 默認就是拷貝源文件過來
    • 並最終將所有在image.xcassets中的文件打包成一個文件
    • 同時也打包到軟件安裝包中
    • 默認就帶有緩存,代碼塊或者圖片文件使用結束時,不會釋放圖片所佔內存
    • 圖片在代碼中訪問,不需要寫後綴名;
    imageView.image = [UIImage imageNamed:@"2"];

4. UIImageView-幀動畫的基本使用

  • 設置多張圖片–演示事例程序<拳皇>
  • 幀動畫(連續播放圖片)

    • 站立

    設置動畫圖片—>開始動畫–(無限循環)–>設置播放次數–(設置1次,圖片消失)–>設置第一張圖片

  • 大招
    圖片被拉伸,設置內容模式(Bottom Left)
  • UIImageView的常見屬性

    @property(nonatomic,retain) UIImage *image; 
    //顯示的圖片
    
    @property(nonatomic,copy) NSArray *animationImages; 
    //顯示的動畫圖片
    
    @property(nonatomic) NSTimeInterval animationDuration; 
    //動畫圖片的持續時間
    
    @property(nonatomic) NSInteger      animationRepeatCount; 
    //動畫的播放次數(默認是0,代表無限播放)
  • UIImageView的常見方法

    - (void)startAnimating; // 開始動畫
    - (void)stopAnimating; // 停止動畫
    - (BOOL)isAnimating; // 是否正在執行動畫
  • 如何抽取重複代碼?

    • 將重複的東西寫入新方法
    • 先把相同的代碼抽到方法中
    • 把要變化的東西換成變量,然後編譯,把報錯的設置爲方法的參數
  • 示例:

    
    #pragma mark  - 播放幀動畫
    
    - (void )playWithName:(NSString *)name andImageNumber:(int)number{
      // 1. 創建圖片
      // 1.1 創建可變數組,存儲圖片
      NSMutableArray *tempImage = [[NSMutableArray alloc]init];
      // 1.2 通過遍歷獲取圖片
    
      for (int i = 0 ; i < number ; i++) {
          // 1.2.1. 獲取圖片名稱
          NSString * imageName = [NSString stringWithFormat:@"%@_%i", name ,i+1];
          // 1.2.2. 創建對象,並獲取圖片
          UIImage * image = [UIImage imageNamed:imageName];
          // 1.2.3. 將圖片存入數組中
          [tempImage addObject:image];
      }    
      // 2. 設置動畫圖片(將一個僅存儲了該動作所有圖片的數組賦值給動畫屬性)
      self.imageView.animationImages = tempImage;
    
      // 3. 設置動畫的次數
      self.imageView.animationRepeatCount = [name isEqualToString:@"stand"] ? 0 : 1;
    
      // 4. 設置動畫的時間
      self.imageView.animationDuration = number * 0.05;
    
      // 5. 開啓動畫
      [self.imageView startAnimating];
    
      // 6. 判斷是否是站立的動作,如果不是則動畫結束後立即播放站立動畫
      if (([@"stand" isEqualToString:name] ? 0 : 1) ){
    //        // 7. 設置(最後)顯示的圖片
    //        self.imageView.image = [UIImage imageNamed:@"stand_1"];
          // 7.大招動畫播放完畢後,播放站立動畫
          // self.imageView.animationDuration 時間後執行self的stand的方法
          [self performSelector:@selector(stand) withObject:nil  afterDelay:self.imageView.animationDuration];        
      }
    }

  • 5. UIImageView-播放音頻文件

    • 導入框架

      
      #import <AVFoundation/AVFoundation.h>
      
    • 播放音頻

      // 在類擴展中聲明 播放器
      @property (strong ,nonatomic)AVPlayer *player;
      // 8.獲取軟件安裝包對象
      NSBundle *bundle = [NSBundle mainBundle];
      
      // 9.返回軟件安裝包中某一個資源的路徑(這裏查找並返回 "dazhao.mp3" 音頻文件路徑)
      NSURL *url = [bundle URLForResource:@"dazhao" withExtension:@"mp3"];
      
      // 10.創建播放器對象
      self.player = [[AVPlayer alloc]initWithURL:url];
      
      // 11.播放音頻(調用對象方法)
      [self.player play];

    6. UIImageView-加載無緩存的圖片

    • 分析內存問題

      • 看Memory(放大招,內存飆升,但是結束後,內存並沒有降下來)
    • 原因: imageNamed: 默認緩存圖片

      • 蘋果爲什麼這麼幹?

      方便下次使用,下次訪問圖片直接去內存,不需要再讀取文件,加快訪問速度

  • 緩存的優點和缺點

    • 優點:如果資源是經常用的,乾脆放在內存中,不需要耗時的讀寫操作
    • 缺點:緩存是會越積越多的,佔用內存
  • 示例

    • 有緩存
    // 1.2.2. 創建對象,並從xcassets中獲取圖片,存儲在這裏的圖片會被壓縮打包,無法通過全路徑獲取,
    // 使用之後存在緩存區中,暫時不被釋放,蘋果官方推薦這麼使用,這樣軟件的響應相對無緩衝快;
    UIImage * image = [UIImage imageNamed:imageName];
    • 無緩存
    // 獲取圖片的全路徑,這裏的圖片是放在Supporting Files
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
    
    // 1.2.2. 創建圖片對象,並根圖片的全路徑獲取圖片,通過全路徑加載獲取到的圖片在使用之後釋放空間,即是無緩存,
    // 每次使用的時候都要重新加載,這樣軟件的響應相對有緩存慢;該方法一般用於使用次數不多的情況;
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
  • 注意點

    1. 放在images.xcassets中的圖片,只能通過文件名訪問,沒有全路徑
    2. 大批量的圖片不要放在images.xcassets中,默認就帶有緩存

  • 7. UIImageView-使用總結

    • 圖片的兩種加載方式

      • 有緩存
        1. 使用場合:圖片比較小、使用頻率比較高
        2. 建議:把需要緩存的圖片放到Images.xcassets
      UIImage *image =[UIImage imageNamed:@"圖片名"];
      • 沒有緩存
        1. 使用場合:圖片比較大,使用頻率比較低
        2. 建議:不需要緩存的圖片不能放在Images.xcassets中
      NSString *file = [[NSBundle mainBundle] pathForResource:@"圖片名" ofType:@"圖片擴展名"];
      UIImage *image = [UIImage imageWithContentOfFile:file];    
      //只要方法名帶有file的,都是傳全路徑
      • 注意: 放到Image.xcassets中的圖片只能通過圖片名去加載,蘋果會壓縮圖片,而且默認帶有緩存
    • 延遲做一些事情

      [objc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
      // 10秒後調用objc的stand:方法,並且傳遞@“123”參數
      // objc可以是任意對象
    • 音頻文件的簡單播放

      // 創建一個音頻文件的URL(URL就是文件的路徑對象)方法一:
      NSURL *url = [[NSBundle mainBundle] URLForResource:@"音頻文件名" withExtention:@"音頻文件擴展名"];
      //創建一個音頻文件的URL(URL就是文件的路徑對象)方法二:
      NSURL *url = [[NSBundle mainBundle] URLForResource:@"音頻文件名.音頻文件擴展名" withExtention:nil];
      // 創建播放器
      self.palyer = [AVPlayer playerWithURL:url];
      [self.player play];

    補充:

    1. 從安裝包中獲取某一個資源的路勁

    • 很多資源都是加載項目中的,項目中的資源都是通過mainBundle來獲取的

      // 1.創建獲取軟件安裝包對象
      NSBundle *bundle = [NSBundle mainBundle];
      
      // 2.返回軟件安裝包中某一個資源的路徑(這裏查找並返回 "dazhao.mp3" 音頻文件路徑)
      NSURL *url = [bundle URLForResource:@"dazhao.mp3" withExtension:nil];

    發佈了34 篇原創文章 · 獲贊 12 · 訪問量 5萬+
    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章