一.常用屬性
- 1、
image
: default is nil。圖片屬性 - image的格式可以是.png或者.jpg的,但是后綴名為.jpg格式的圖片只能放Assets.xcassets文件夾里才有效,而.png格式的圖片放外面也可以.
- 圖片的兩種加載方式
- 有緩存
UIImage *image =[UIImage imageNamed:@"圖片名"];
使用場合:圖片比較小、使用頻率比較高
建議:把需要緩存的圖片放到Assets.xcassets- 沒有緩存
NSString *file = [[NSBundle mainBundle] pathForResource:@"圖片名" ofType:@"圖片擴展名"];
UIImage *image = [UIImage imageWithContentOfFile:file];
*只要方法名帶有file的,都是傳全路徑
使用場合:圖片比較大,使用頻率比較低
建議:不需要緩存的圖片不能放在Assets.xcassets中- 放到Assets.xcassets中的圖片只能通過圖片名去加載,蘋果會壓縮圖片,而且默認帶有緩存
- 2、
highlightedImage
: default is nil。高亮狀態圖片屬性 - 3、
userInteractionEnabled
: default is NO。用戶是否可以交互屬性 - 4、
highlighted
: default is NO。判斷圖片是否是高亮狀態 - 5、
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,
};
經驗:
- 1.帶有scale單詞的圖片有可能被拉伸:
- UIViewContentModeScaleToFill:將圖片拉伸填充整個imageView;圖片顯示的 尺寸跟imageView的尺寸是一樣的.
- 2.帶有scale單詞的,并且帶有aspect單詞的:可能會被拉伸,但是會保持圖片原來的寬高比
- UIViewContentModeScaleAspectFit:保證剛好能看到圖片的全部
- UIViewContentModeScaleAspectFull:拉伸至圖片的寬度或者高度跟imageView一樣.
- 3.不帶有scale單詞的,圖片絕對不會被拉伸,保持圖片原來的寬度和高度
下面屬性允許圖像組動畫,該數組內可能包含相同的多個副本
- 5、animationImages: 該數組當中必須包含多張圖片,設置單張圖片將被隱藏,默認 nil。
@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages;
- 6、highlightedAnimationImages: 高亮狀態的組動畫。
- 7、animationDuration: 動畫播放時間,對于一個周期的圖像,默認的是圖像是一秒30幀。
@property (nonatomic) NSTimeInterval animationDuration;
- 8、animationRepeatCount: 0 means infinite (default is 0)。動畫循環次數。0意味著無限(默認0,代表無限播放)。
@property (nonatomic) NSInteger animationRepeatCount;
9、tintColor: 給控件內子視圖設置顏色。詳細信息請見 UIImage類屬性。
10、focusedFrameGuide: 如果設置了
adjustsImageWhenAncestorFocused
,圖像視圖可以在一個更大的 frame 中顯示其圖片的焦點。這個布局指南,可用于將其他元素與圖像視圖的聚焦幀對齊。
@property(readonly,strong) UILayoutGuide *focusedFrameGuide;
二.常用方法
- 1、
initWithImage:
: 構造方法,在初始化對象時直接進行默認圖片進行賦值,默認尺寸就是圖片的尺寸,位置默認從(0,0)開始。
- (instancetype)initWithImage: (nullable UIImage *)image;
- 2、
initWithImage:highlightedImage:
: 構造方法,在初始化對象時直接給默
認和高亮圖片進行賦值
- (instancetype)initWithImage: (nullable UIImage *)image highlightedImage: (nullable UIImage *)highlightedImage;
- 3、
startAnimating:
: 開始動畫
- (void)startAnimating;
- 4、
stopAnimating:
: 結束動畫
- (void)stopAnimating;
- 5、
isAnimating:
: 是否在動畫中
- (BOOL)isAnimating;