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];?//?給圖片添加手勢