UIImageView

UIImageView

summary

UIImageView極其常用,功能比較專(zhuān)一:顯示圖片

pooperty

@property(nonatomic,retain) UIImage     *image; 
顯示的圖片
@property(nonatomic,copy) NSArray       *animationImages; 
顯示的動(dòng)畫(huà)圖片
@property(nonatomic) NSTimeInterval     animationDuration; 
動(dòng)畫(huà)圖片的持續(xù)時(shí)間
@property(nonatomic) NSInteger          animationRepeatCount; 
動(dòng)畫(huà)的播放次數(shù)(默認(rèn)是0,代表無(wú)限播放)
@property(nonatomic) UIViewContentMode  contentMode; 
內(nèi)容模式: 一般用來(lái)控制圖片如何顯示

Method

- (void)startAnimating; // 開(kāi)始動(dòng)畫(huà)
- (void)stopAnimating; // 停止動(dòng)畫(huà)
- (BOOL)isAnimating; // 是否正在執(zhí)行動(dòng)畫(huà)

UIImageView-設(shè)置imageView的frame

initWithImage 
默認(rèn)尺寸就是圖片的尺寸,位置默認(rèn)從(0,0)開(kāi)始
imageView.frame = CGRectMake(100,100, image.size.width, image.size.height);
注意尺寸不能設(shè)置在圖片之前(演示)

錯(cuò)誤代碼:imageView.frame.size.width = image.size.width
直接賦值size,但會(huì)出現(xiàn)OC語(yǔ)法錯(cuò)
原因:不能直接修改OC對(duì)象結(jié)構(gòu)體屬性的成員
結(jié)構(gòu)體是值傳遞

如何賦值?
CGRect tempFrame = imageView.frame; // frame是一個(gè)新定義的變量
tempFrame =  image.size;
imageView.frame = tempFrame; // 如果少了這一句(不是對(duì)象,是結(jié)構(gòu)體)

常見(jiàn)寫(xiě)法
imageView.frame = (CGRect){CGPointMake(100,100), imageView.image.size};
imageView.frame = (CGRect){CGPointMakeZero, imageView.image.size};
修改frame的3種方式(同樣適用于bounds/center)
  • 1.直接使用CGRectMake函數(shù)
  • 2.利用臨時(shí)結(jié)構(gòu)體變量
  • 3.直接運(yùn)用結(jié)構(gòu)體賦值

UIImage類(lèi)

一個(gè)UIImage對(duì)象代表一張圖片對(duì)象
  • Method
 加載圖片的方式:
   1. imageNamed:
   2. imageWithContentsOfFile:
 
   1. 加載Assets.xcassets這里面的圖片:
    1> 打包后變成Assets.car
    2> 拿不到路徑
    3> 只能通過(guò)imageNamed:來(lái)加載圖片
    4> 不能通過(guò)imageWithContentsOfFile:來(lái)加載圖片
 
   2. 放到項(xiàng)目中的圖片:
    1> 可以拿到路徑
    2> 能通過(guò)imageNamed:來(lái)加載圖片
    3> 也能通過(guò)imageWithContentsOfFile:來(lái)加載圖片

返回一張受保護(hù)且被拉伸的圖片

iOS 5.0以前使用(棄用)這個(gè)方法會(huì)自動(dòng)計(jì)算出偏向中間的一個(gè)1*1的方格也就是被拉伸的地方(默認(rèn)使用拉伸)

[image stretchableImageWithLeftCapWidth:imageHeight *0.5 topCapHeight:imageHeight *0.5 ];

新方法

 [image resizableImageWithCapInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
 //拉伸模式 UIImageResizingModeTile平鋪 UIImageResizingModeStretch拉伸
 [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageheight * 0.5, imagewidth * 0.5, imageheight * 0.5 -1, imagewidth * 0.5 - 1) resizingMode:UIImageResizingModeTile];

contentMode屬性

  • 帶有scale單詞的 <圖片有可能被拉深>
  • UIViewContentModeScaleToFill
  • 將圖片拉伸填充整個(gè)imageView
  • 圖片顯示的尺寸跟imageView的尺寸是一樣的
  • 帶有aspect單詞的:保持圖片原來(lái)的寬高比
  • UIViewContentModeScaleAspectFit
  • 保證剛好能看到圖片的全部
  • UIViewContentModeScaleAspectFill<畫(huà)圖分析>
  • 拉伸至圖片的寬度或者高度跟imageView一樣
  • 沒(méi)有帶有scale的單詞<圖片絕對(duì)不會(huì)被拉伸>

裁剪

//居中顯示
imageView.contentMode = UIViewContentModeCenter;
// 裁剪超出imageView邊框的部分
imageView.clipsToBounds = YES;

UIImageView-幀動(dòng)畫(huà)的基本使用

demo

//創(chuàng)建UIImageView根據(jù)圖片初始化大小
    UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    //讓圖片居中
    images.center = CGPointMake(self.view.bounds.size.width *0.5, self.view.bounds.size.height *0.5);
    //把image賦給imageView
    images.image =image;
    //加到控制器的View中
    [self.view addSubview:images];
    //創(chuàng)建圖片的數(shù)組
    NSMutableArray *imagesArr = [NSMutableArray array];
    //循環(huán)添加放入數(shù)組
    for (int i = 0; i < 20; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i+1];
        UIImage *ima = [UIImage imageNamed:imageName];
        [imagesArr addObject:ima];
    }
    //讓全局變量的imageView賦值
    self.imageView = images;
    //讓全局變量的arr賦值
    self.arr = imagesArr;
    //imageView透明度
    self.imageView.alpha = 0.5;
    // 設(shè)置播放時(shí)長(zhǎng)
    // 1秒30幀, 一張圖片的時(shí)間 = 1/30 = 0.03333 20 * 0.0333
    self.imageView.animationDuration = 1.0;
    // 開(kāi)始動(dòng)畫(huà)
    [self.imageView startAnimating];

UIImageView-加載圖片的緩存問(wèn)題

  • imageNamed:
  • 有緩存
    • UIImage *image =[UIImage imageNamed:@"圖片名"];
    • 使用場(chǎng)合:圖片比較小、使用頻率比較高
    • 建議:把需要緩存的圖片放到Image.xcassets
  • 沒(méi)有緩存
    • NSString *file = [[NSBundle mainBundle] pathForResource:@"圖片名" ofType:@"圖片擴(kuò)展名"];
    • UIImage *image = [UIImage imageWithContentOfFile:file];
    • 只要方法名帶有file的,都是傳全路徑
    • 使用場(chǎng)合:圖片比較大,使用頻率比較低
    • 建議:不需要緩存的圖片不能放在Assets.xcassets中

總結(jié)

1.放在Assets.xcassets中的圖片,只能通過(guò)文件名訪問(wèn),沒(méi)有全路徑
2.大批量的圖片不要放在Assets.xcassets中,默認(rèn)就帶有緩存
3.放到Assets.xcassets中的圖片只能通過(guò)圖片名去加載,蘋(píng)果會(huì)壓縮圖片,而且默認(rèn)帶有緩存
4.很多資源都是加載項(xiàng)目中的,項(xiàng)目中的資源都是通過(guò)mainBundle來(lái)獲取的

延遲做一些事情

    //iOS中有很多方式(GCD,dispatch...)
    [abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
    // 10秒后調(diào)用abc的stand:方法,并且傳遞@“123”參數(shù)
    // abc可以是任意對(duì)象
    // NSTimeInterval -->進(jìn).h文件分析--->double--->秒

音頻文件的簡(jiǎn)單播放

 // 創(chuàng)建一個(gè)音頻文件的URL(URL就是文件的路徑對(duì)象)
     NSURL *url = [[NSBundle mainBundle] URLForResource:@"音頻文件名" withExtention:@“音頻文件擴(kuò)展名”];
    //另一寫(xiě)法
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"音頻文件名.音頻文件擴(kuò)展名" withExtention:nil];
    // 創(chuàng)建播放器
    self.palyer = [AVPlayer playerWithURL:url];
    [self.player play];

Demo

demo1 初始化frame

//方式一 在初始化以后賦值frame
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageNamed:@"2"];
imageView.frame = CGRectMake(10, 10, 100, 200);
imageView.frame = (CGRect){{10,10},{200,200}};
imageView.contentMode = UIViewContentModeScaleAspectFill;

//方式二 初始化的時(shí)候賦值frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 200)];
UIImage *imgae = [UIImage imageNamed:@"2"];
imageView1.image = imgae;

//方式三 根據(jù)圖片的寬高初始化
UIImage *imgae = [UIImage imageNamed:@"2"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:imgae];
//缺點(diǎn)  移動(dòng)的時(shí)候只能通過(guò)center移動(dòng)
imageView.center = CGPointMake(200, 300);

//方式四
UIImage *image = [UIImage imageNamed:@"2"];
UIImageView *imgaeView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, image.size.width, image.size.height)];
imgaeView.image = image;
[self.view addSubview:imgaeView];



demo2 裁剪

// 1.1 創(chuàng)建UIImageView對(duì)象
    UIImageView *imageView = [[UIImageView alloc]init];
    // 1.2 設(shè)置frame
    imageView.frame = self.view.bounds;
    // 1.3 設(shè)置背景
    imageView.backgroundColor = [UIColor redColor];
    // 1.4 設(shè)置圖片 (png不需要后綴)
    imageView.image = [UIImage imageNamed:@"1"];
    /**
     
     UIViewContentModeRedraw, // 重新繪制 (核心繪圖) drawRact
     
     //帶有Scale,標(biāo)明圖片有可能被拉伸或壓縮
     UIViewContentModeScaleToFill, // 完全的壓縮或拉伸
     
     // Aspect 比例,縮放是帶有比例的
     UIViewContentModeScaleAspectFit, // 寬高比不變 Fit 適應(yīng)
     UIViewContentModeScaleAspectFill, // 寬高比不變 Fill 填充
     
     //不帶有Scale,標(biāo)明圖片不可能被拉伸或壓縮
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
     */
    // 1.5 設(shè)置圖片的內(nèi)容模式
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    // 2.0 加到控制器的view中
    [self.view addSubview:imageView];
    // 裁剪多余的部分
    imageView.clipsToBounds = YES;
    ```
`demo3 毛玻璃`
```objc
// 1.創(chuàng)建UIImageView對(duì)象
    UIImageView *image = [[UIImageView alloc]init];
    // 2. 設(shè)置尺寸
    //image.frame = CGRectMake(0, 0,self.view.bounds.size.width , self.view.bounds.size.height);
    image.frame = self.view.bounds;
    NSLog(@"%@",NSStringFromCGRect(image.frame));
    // 3. 設(shè)置背景顏色
    image.backgroundColor = [UIColor yellowColor];
    // 4. 設(shè)置背景圖片
    image.image = [UIImage imageNamed:@"1"];
    // 5.設(shè)置圖片的內(nèi)容模式
    image.contentMode = UIViewContentModeScaleAspectFill;
    
    // 6.加毛玻璃
    // 6.1 創(chuàng)建UIToolBar對(duì)象
    UIToolbar *tob = [[UIToolbar alloc]init];
    
    // 6.2 設(shè)置toolBar的frame
    tob.frame = image.frame;
    // 6.3 設(shè)置毛玻璃的樣式
    tob.barStyle = UIBarStyleBlack;
    tob.alpha = 0.85;
    // 6.4 加到imageView中
   
    [self.view addSubview:image];
    [self.view addSubview:tob];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容