iOS 開發(fā)-UIImageViews(圖片)的使用

UIImageViews的基本使用

1. 創(chuàng)建imageview

? ??UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImage"]];?

????創(chuàng)建并設置默認圖, 也可以

? ? UIImageView*imageView = [[UIImageView alloc] init];

? ? imageView.image= [UIImageimageNamed:@"bgImage"];

? ? 還可以這樣先設置imageview的大, 在設置圖片

? ??UIImageView*imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0,144,SCREEN_Width,50))];

? ? imageView.image= [UIImageimageNamed:@"bgImage"];

? ? 由此可看imageview的frame可以這樣設置

? ??imageView.frame=CGRectMake(0,144,SCREEN_Width,50);

? ? 通常我們使用的的imageview都會添加圓角邊框

????imageView.layer.masksToBounds = YES;

? ? imageView.layer.cornerRadius=25;

????imageView.layer.borderColor = [UIColor blueColor].CGColor;

? ? imageView.layer.borderWidth=1;

? ? 這個圓角和邊框像view和label以及button的設置方式都是一樣的 當然imageview也一樣

? ??imageView.backgroundColor= [UIColorclearColor]; 圖片設置背景顏色, 我通常使用clearColor ?透明

? ??imageView.userInteractionEnabled = YES; 圖片設置成可交互, 設置為NO則不能交互

? ??[self.viewaddSubview: imageView]; 添加視圖也可叫做顯示視圖

? ? 設置圖片內(nèi)容的布局方式imageView.contentMode

? ??這個屬性是用來設置圖片的顯示方式,如居中、居右,是否縮放等

? ??imageView.contentMode = UIViewContentModeScaleAspectFit;

? ??UIViewContentMode contentMode枚舉類型

? ? ? ? (1)?UIViewContentModeScaleToFill;? ??默認,對圖片進行拉伸處理(不是按比例),是充滿bouns

? ? ? ? (2)?UIViewContentModeScaleAspectFit;? ??按原圖比例進行拉伸,是圖片完全展示在bouns中

? ? ? ? (3)?UIViewContentModeScaleAspectFill;? ??按原圖比例填充,使圖片展示在bouns中,可能只顯示部分

? ? ? ? (4)?UIViewContentModeRedraw;? ??重劃邊界變化(重設 - setNeedsDisplay)

? ? ? ? (5)?UIViewContentModeCenter;? ??圖片顯示在imageview的正中間,原圖大小

? ? ? ? (6)?UIViewContentModeTop;? ??圖片顯示在imageview的上部,原圖大小

? ? ? ? (7)?UIViewContentModeBottom;? ??圖片顯示在imageview的下部,原圖大小

? ? ? ? (8)?UIViewContentModeLeft;? ??圖片顯示在imageview的左部,原圖大小

? ? ? ? (9)?UIViewContentModeRight;? ??圖片顯示在imageview的右部,原圖大小

? ? ? ? (10)?UIViewContentModeTopLeft;? ??圖片顯示在imageview的左上部,原圖大小

? ? ? ? (11)?UIViewContentModeTopRight;? ??圖片顯示在imageview的右上部,原圖大小

? ? ? ? (12)?UIViewContentModeBottomLeft;? ??圖片顯示在imageview的左下部,原圖大小

? ? ? ? (13)?UIViewContentModeBottomRight;? ??圖片顯示在imageview的右下部,原圖大小

? ??imageView.alpha = 1.0; ? ?設置圖片透明度

2. 圖片設置加載動畫

? ??????NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];

? ??????NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];

? ??????NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];

? ??????imageView.animationImages = @[[UIImage imageWithContentsOfFile:path1],[UIImage imageWithContentsOfFile:path2],[UIImage imageWithContentsOfFile:path3]];

? ??????imageView.animationDuration = 5.0f; ? ?設置循環(huán)一次的時間

? ??????imageView.animationRepeatCount = 0;? ? // 設置循環(huán)次數(shù)(0為無線循環(huán))

? ??????[imageView startAnimating];? ? ? ? ? ? // 開始動畫

? ??????[imageView stopAnimating];? ? ? ? ? ? ? // 停止動畫

3.?使用imageWithData

? ??NSData *imageData = [NSData dataWithContentsOfFile:path];

? ? UIImage *image4 = [UIImage imageWithData:imageData];

4.?使用imageWithContentsOfFile

? ??NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];

? ? UIImage *image2 = [UIImage imageWithContentsOfFile:path];

5. UIimageview拓展

? ??ImageView.hidden?=?NO;????隱藏或者顯示圖片?YES為隱藏

????[ImageView?sizeToFit];????將圖片尺寸調(diào)整為與內(nèi)容圖片相同

? ??UITapGestureRecognizer?*singleTap = [[UITapGestureRecognizer?alloc]?initWithTarget:self?action:@selector(tapImageView:)];?//?設置手勢

??[ImageView?addGestureRecognizer:singleTap];?//?給圖片添加手勢

此上就是UIimageview在使用中的常用屬性以及使用方法, 如有不足或者錯誤, 歡迎大家前來糾正, 謝謝

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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