iOS階段學習第27天筆記(UIButton-UIImageView的介紹),ios計算uiimage大小

來源:互聯網
上載者:User

iOS階段學習第27天筆記(UIButton-UIImageView的介紹),ios計算uiimage大小

iOS學習(UI)知識點整理

一、關於UIButton的介紹

1)概念:UIButton 是一種常用的控制項,通過點擊觸發相應的功能

2)UIButton 的幾種常用的狀態
        1、UIControlStateNormal  正常狀態
        2、UIControlStateHighlighted 高亮狀態
        3、UIControlStateSelected 選中狀態  -> 當button的selected設定成yes之後才能觸發

3)UIButton常用的幾種事件
      1、UIControlEventTouchUpInside  按鈕按下並抬起事件
      2、UIControlEventTouchDown   按鈕按下事件
      3、UIControlEventTouchDownRepeat 按鈕多次點擊觸發事件

4)UIButton 初始化執行個體代碼

 1 UIButton *button = [[UIButton alloc] init]; 2 button.frame = CGRectMake(20, 50, 50 , 50); 3 button.backgroundColor = [UIColor clearColor]; 4 [button setTitle:@"按鈕1 正常狀態" forState:UIControlStateNormal]; 5 [button setTitle:@"按鈕1 高亮狀態" forState:UIControlStateHighlighted]; 6 [button setTitle:@"按鈕1 選中狀態" forState:UIControlStateSelected]; 7  8 //按鈕點擊時觸發事件 9 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];10 //按鈕按下後觸發事件11 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDown];12 //按鈕雙擊觸發事件13 [button addTarget:self action:@selector(buttonTappedDown:) forControlEvents:UIControlEventTouchDownRepeat];14 //設定按鈕高亮狀態下的字型顏色15 [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];16 //button字型變為35號加粗的字型17 button.titleLabel.font = [UIFont boldSystemFontOfSize:35];18 //設定圓角    19 button.layer.cornerRadius = 5.f;20 //設定邊框寬度21 button.layer.borderWidth = 2.1;22 //設定邊框顏色23 button.layer.borderColor = [UIColor lightGrayColor].CGColor;24  //設定按鈕背景圖   25 UIImage *imageNormal = [UIImage imageNamed:@"camera"];26 //設定imageNormal為按鈕的正常情況的圖片27 [button setImage:imageNormal forState:UIControlStateNormal];28     29 UIImage *imageHightLight = [UIImage imageNamed:@"camera2"];30 //設定imageHightLight為按鈕的高亮情況的圖片31 [button setImage:imageHightLight forState:UIControlStateHighlighted];32 //當button設定了圖片的時候 並且沒有設定高亮狀態下得圖片,取消高亮狀態, 預設是Yes33 button.adjustsImageWhenHighlighted = YES;    34 [self.window addSubview:button];

 
5)防止按鈕多次點擊重複提交資料的執行個體代碼

 1 [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];  3 - (void)buttonTapped:(UIButton *)button 4 { 5     //設定按鈕不可點擊 6     button.userInteractionEnabled = NO;   8     //順延強制方法 防止按鈕被快速點擊或者不希望點擊造成錯誤 9     [self performSelector:@selector(delayMethod:) withObject:button afterDelay:1]; 11 }12 13 //延遲方法->設定按鈕為可點擊狀態14 - (void)delayMethod:(UIButton *)button15 {16     button.userInteractionEnabled = YES;    17 }

 

二、關於UIImageView的介紹

1)概念:UIImageView 是iOS中專門用於展示圖片的控制項

2)UIImageView 初始化 執行個體代碼

 1     UIImageView *imageView = [[UIImageView alloc] init]; 2     imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width); 3     imageView.backgroundColor = [UIColor whiteColor]; 4     imageView.center = self.view.center; 5  6     //tag設定控制項的唯一標識,值不能重複 7     imageView.tag = 100; 8  9     //UIImageView的 clipsToBounds屬性,設定為yes的時候超出部分,不予以顯示10     imageView.clipsToBounds = YES;11 12     //讀取一張圖片13     UIImage *image = [UIImage imageNamed:@"icon"];14     imageView.image = image;15 16     //設定圖片展示模式17     imageView.contentMode = UIViewContentModeScaleAspectFill;18 19     //開啟imageview的使用者互動 註:要實現圖片點擊事件此屬性必須設定為YES20     imageView.userInteractionEnabled = YES;21     [self.view addSubview:imageView];22 23     //為UIImageView添加點擊事件  24     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
action:@selector(imageViewTapped:)];
25 [imageView addGestureRecognizer:tap];

 
3)UI_ImageView中常用的幾種填充模式
   1、UIViewContentModeScaleToFill  展開image使其充滿UIImageView
   2、UIViewContentModeScaleAspectFill 展開image使其不變形,並且充滿UIImageView
   3、UIViewContentModeScaleAspectFit 展開imgage使其不變形,並且完全顯示在UIImageView中

4)UITapGestureRecognizer  除了可以給UI_ImageView添加點擊方法外還可以給其他控制項添加點擊方法
     如:UI_Lable、UI_View...等

5)iOS中擷取圖片的三種方法
 方法一: 

1 //把圖片對象載入到記憶體中2 UIImage *image1 = [UIImage imageNamed:@"camera"];3 CGSize size = image1.size;4 NSLog(@"size.w %f   size.h %f",size.width ,size.height);5 //如果圖片的格式是png,則尾碼名可以省略,其他格式不能省略6 UIImage *image2 = [UIImage imageNamed:@"icon.jpeg"];

方法二: 

//使用情境:讀取大圖片,比較占記憶體的,需要及時釋放的圖片要用這種方法 //讀取icon.jpegNSString *imagePath3 = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];UIImage *image3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];NSString *imagePath3_1 = [[NSBundle mainBundle] pathForResource:@"icon.jpeg" ofType:nil];UIImage *image3_1 = [[UIImage alloc] initWithContentsOfFile:imagePath3_1];

方法三:

1 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];2 3  UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.