UIImageView


一、UIImageView 基本屬性

  • 1 創(chuàng)建UIImageView對(duì)象
UIImageView *imageView = [UIImageView alloc] init];
  • 2 設(shè)置尺寸
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
  • 3 設(shè)置背景顏色
imageView.backgroundColor = [UIColor redColor];
  • 4 設(shè)置背景圖片
imageView.image = [UIImage imageNamed:@"picture"];
:-> png格式不需要加后綴
  • 5 設(shè)置圖片內(nèi)容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 重新繪制 
UIViewContentModeRedraw, 
//1.帶有Scale,標(biāo)明圖片有可能被完全的拉伸或壓縮 
UIViewContentModeScaleToFill, 
//2.Aspect 比例,縮放是帶有比例的 
UIViewContentModeScaleAspectFit, // 寬高比不變 Fit 適應(yīng) 
UIViewContentModeScaleAspectFill, // 寬高比不變 Fill 填充
//3.不帶有Scale,標(biāo)明圖片不可能被拉伸或壓縮
UIViewContentModeCenter,
UIViewContentModeTop, 
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

二、制作毛玻璃效果

1 創(chuàng)建UIToolBar 對(duì)象

UIToolBar *toolBar = [[UIToolBar alloc] init];

2 設(shè)置toolBar的frame

toolBar.frame = imageView.bounds;

3 設(shè)置毛玻璃的樣式

toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.95;

4 加載到父控制器(這里是imageView)中

[self.image addSubview:toolBar];

三、圖片拉伸問題

  • 1 創(chuàng)建UIImage對(duì)象
UIImage *image = [UIImage imageNamed:@"image_name"];
  • 2 獲取image的尺寸
CGFloat imagWidth = image.size.width;
CGFloat imageHeight = image.size.height;
  • 3 返回一張受保護(hù)而且拉伸的圖片
  • 方式一
>>3.1
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1)];
>>3.2
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1) resizingMode:UIImageResizingModeTile];
//UIImageResizingModeTile, 平鋪(常用)
//UIImageResizingModeStretch, 拉伸(伸縮)
  • 方式二
//右邊需要保護(hù)的區(qū)域 = 圖片的width - leftCapWidth - 1
// bottom cap =  height - topCapHeight - 1
UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeight * 0.5];

四、圖片資源存放問題

  • 1 方式一
self.imageView.image = [UIImage imageNamed:@"1"];
  • 2 方式二
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
self.imageView.image = [UIImage imageWithContentsOfFile:path];
  • 3 總結(jié)
加載圖片有兩種方式:

1.通過 imageNamed:來加載
2.通過 imageWithContentsOfFile:來加載

3.1 放到Assets.xcassets這里面的圖片:
1> 打包后變成Assets.car
2> 拿不到路徑
3> 只能通過imageNamed:來加載圖片
4> 不能通過imageWithContentsOfFile:來加載圖片
3.2 放到項(xiàng)目中的圖片:
1> 可以拿到路徑
2> 能通過imageNamed:來加載圖片
3> 也能通過imageWithContentsOfFile:來加載圖片
注意:

1>經(jīng)常用的圖片建議用imageNamed:
2>不經(jīng)常用的通過imageWithContentsOfFile:來加載圖片

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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