[ocUI日記]UIImage和UIImageview,ocuiuiimage

來源:互聯網
上載者:User

[ocUI日記]UIImage和UIImageview,ocuiuiimage

UIImage

 

 1     //用類方法建立uiimage(png格式可以省略尾碼) 2     UIImage *image1 = [UIImage imageNamed:@"001"]; 3     //用路徑來獲得圖片 4     NSString *path = @"";//這是路徑地址 5     UIImage *image2 = [UIImage imageWithContentsOfFile:path]; 6     //根據網址來獲得圖片 7     NSString *url = @"";//這是網址 8     UIImage *image3 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; 9     //uiimage繼承自uiview所以uiview的方法他都能用  所以這裡就不在一一列舉10     float a = image1.size.width;//獲得圖片的寬度11     float b = image1.size.height;//獲得圖片的高度12     13     14     //nsdata和uiimage之間的轉換15     //nsdata 轉換成 uiimage16     NSData *imagedata = [NSData dataWithContentsOfFile:path];17     UIImage *image = [UIImage imageWithData:imagedata];18     //uiimage 轉化成nsdata19     NSData *imagedata1 = [NSData dataWithContentsOfFile:path];

但是寫到這裡,我們發現我們並不能講圖片顯示到螢幕上,並不是因為我們沒有寫add方法,而是image並不能直接添加到self.view上,於是就有了下面要講的內容

UIImageView

 1     //在建立uiimageview的同時添加一個參數進去 2     UIImageView *view1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"001"]]; 3     //設定uiimageview的位置和大小 4     view1.frame = CGRectMake(20, 20, 100, 100); 5     //設定imageview的大小和image的大小相適應 6     [view1 sizeToFit]; 7     //添加到視圖上 8     [self.view addSubview:view1]; 9     //imageview的contentMode屬性:這個屬性是用來設定圖片的顯示方式,如置中、居右,是否縮放等10     11     /*12      UIViewContentModeScaleToFill 13      UIViewContentModeScaleAspectFit 14      UIViewContentModeScaleAspectFill 15      UIViewContentModeRedraw 16      UIViewContentModeCenter 17      UIViewContentModeTop 18      UIViewContentModeBottom 19      UIViewContentModeLeft 20      UIViewContentModeRight 21      UIViewContentModeTopLeft 22      UIViewContentModeTopRight 23      UIViewContentModeBottomLeft 24      UIViewContentModeBottomRight25      */26     //凡是沒有帶Scale的,當圖片尺寸超過 ImageView尺寸時,只有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會導致圖片變形。 UIViewContentModeScaleAspectFit會保證圖片比例不變,而且全部顯示在ImageView中,這意味著ImageView 會有部分空白。UIViewContentModeScaleAspectFill也會證圖片比例不變,但是是填充整個ImageView的,可能只有部 分圖片顯示出來27     view1.contentMode = UIViewContentModeScaleAspectFill;

接下來給大家帶來一個圖片輪播的demo

    //建立一個uiimageview    UIImageView *imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(140, 200, 100, 100)];    //建立image    UIImage *image1 = [UIImage imageNamed:@"101"];    UIImage *image2 = [UIImage imageNamed:@"201"];    UIImage *image3 = [UIImage imageNamed:@"301"];    UIImage *image4 = [UIImage imageNamed:@"401"];    UIImage *image5 = [UIImage imageNamed:@"501"];    UIImage *image6 = [UIImage imageNamed:@"601"];    UIImage *image7 = [UIImage imageNamed:@"701"];    //將圖片放入數組    NSArray *imagearr = @[image1,image2,image3,image4,image5,image6,image7];    //設定輪播數組    imagev1.animationImages = imagearr;    //設定播放完全部的時間    imagev1.animationDuration = 7;    //設定播放次數  0為無限迴圈    imagev1.animationRepeatCount = 0;    //開始輪播    [imagev1 startAnimating];    [self.view addSubview:imagev1];

由於不會怎麼插入本地視頻,所以就不放展示視頻了,大家見諒

 

相關文章

聯繫我們

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