標籤:
一、UIImageView和UIImage
UIImageView是iOS中用於顯示圖片的類,UIImage是用於儲存圖片資料的類;iOS中幾乎所有看到的圖片資料都儲存在UIImage中,同時所要的圖片都是用UIImageView來顯示;UIImageView和UIImage關係如:
二. 建立UIImageView的兩種方式1.自己設定圖片位置和尺寸
“`
UIImageView *iv = [[UIImageView alloc] init]; //建立的圖片, 沒有預設的寬高
iv.backgroundColor = [UIColor redColor];
UIImage *image = [UIImage imageNamed:@”meinv.jpg”];
iv.image = image;
//自己設定圖片位置和尺寸iv.frame = CGRectMake(100, 100, image.size.width, image.size.height);[self.view addSubview:iv];
“`
2、使用預設圖片尺寸和位置
//預設寬高,為圖片寬高,位置為0,0 UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"meinv.jpg"]]; //推薦設定iv的frame,以便於設定圖片的擺放位置 //iv = CGRectMake(100, 100, image.size.width, image.size.height); iv.backgroundColor = [UIColor redColor]; [self.view addSubview:iv]; NSLog(@"%@",NSStringFromCGRect(iv.frame));
三. ContentMode屬性
typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped. UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay) UIViewContentModeCenter, // contents remain same size. positioned adjusted. UIViewContentModeTop, UIViewContentModeBottom, UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight, UIViewContentModeBottomLeft, UIViewContentModeBottomRight,};規律: 但凡取值中包含Scale單詞的, 都會對圖片進行展開(縮放) 但凡取值中沒有出現Scale單詞的, 都不會對圖片進行展開 UIViewContentModeScaleToFill, > 會按照UIImageView的寬高比來展開圖片 > 直到讓整個圖片都填充UIImageView為止 > 因為是按照UIImageView的寬高比來展開, 所以圖片會變形規律: 但凡取值中包含Aspect單詞的, 都會按照圖片的寬高比來展開 > 因為是按照圖片的寬高比來展開, 所以圖片不會變形 UIViewContentModeScaleAspectFit, > 會按照圖片的寬高比來展開 > 要求整張圖片都必須在UIImageView的範圍內 > 並且寬度和高度其中一個必須和UIImageView一樣 > 置中顯示 UIViewContentModeScaleAspectFill, > 會按照圖片的寬高比來展開 > 要求整張圖片必須填充UIImageView > 並且圖片的寬度或者高度其中一個必須和UIImageView一樣
四. 剪裁超出部分屬性
觀察下面運行效果,理解clipsToBounds屬性 // 1.建立一個UIImageView UIImageView *iv = [[UIImageView alloc] init]; // 2.設定相關屬性 iv.backgroundColor = [UIColor redColor]; iv.image = [UIImage imageNamed:@"meinv.jpg"]; iv.contentMode = UIViewContentModeLeft; // 3.設定frame iv.frame = CGRectMake(100, 100, 200, 200); // 剪下超出的部分 iv.clipsToBounds = YES; [self.view addSubview:iv];
五. UIImageView動畫
- (IBAction)run:(UIButton *)sender{ NSMutableArray *arrM = [NSMutableArray array]; // 1.建立多張圖片 for (int i = 1; i <= 6; i++) { NSString *imageNmae = [NSString stringWithFormat:@"run_%i", i]; UIImage *image = [UIImage imageNamed:imageNmae]; // 2.將所有的圖片放到數組中 [arrM addObject:image]; } // 3.將儲存了所有圖片的數組賦值給UIImageView self.containerView.animationImages = arrM; // 設定重複次數,0,代表無線 self.containerView.animationRepeatCount = 1; //設定一次動畫所需時間 self.containerView.animationDuration = 1; // 開始動畫 [self.containerView startAnimating]; }- (IBAction)stop:(id)sender { //判斷是否正在動畫 if([self.containerView isAnimating]) { //停止動畫 [self.containerView stopAnimating]; }}
六. UIImageView效能最佳化問題
問題描述: 如果直接用 UIImage *image = [UIImage imageNamed:imageNmae];載入圖片,圖片會自動到記憶體中緩衝起來. 這時,當載入多張圖片後,假如執行動畫,就會導致,記憶體暴增,而且當動畫執行完畢之後,也不會釋放。
解決方案://使用initWithContentOfFile:方法直接從mainBundle,app根目錄中載入圖片,//這樣如果遇到上述問題,在執行完動畫之後,圖片會自動釋放..NSString *imageNmae = [NSString stringWithFormat:@"%@_%i", prefix, i];imageNmae = [[NSBundle mainBundle] pathForResource:imageNmae ofType:@"png"];UIImage *image = [[UIImage alloc] initWithContentOfFile:imageName];代替UIImage *image = [UIImage imageNamed:imageNmae];
七. 圖片展開
為什麼要讓圖片展開?
如果我們要設定背景圖片,如按鈕,當我們直接實用圖片時(有時美工也會給我們一張可展開小圖片),可能圖片會被系統展開變形,變得很醜,嚴重影響美觀!
圖片展開就是為瞭解決如上問題而存在的,讓圖片在展開時,保證圖片不變形
圖片展開曆史過程:
- (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [[UIButton alloc]init]; btn.frame = CGRectMake(100, 100, 200, 100); //舊圖片 UIImage *image = [UIImage imageNamed:@"common_button_blue_highlighted"]; //可以指定平鋪還是展開,得到一張新圖片 //指定受保護的地區 UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5); UIImage *newImage = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch]; //按鈕設定背景 [btn setBackgroundImage:newImage forState:UIControlStateNormal]; [self.view addSubview:btn];}
對於圖片的處理還有很多,大家可以上網搜尋,這裡給大家推薦一篇:
http://blog.csdn.net/hastar521/article/details/49122607
iOS UI基礎控制項之UIImageView