iOS基础控件-UIImageView

本文详细介绍了UIImageView的用法,包括创建方式、关键属性如image及其赋值方法,并提到了UIImageView继承自UIView的一些其他属性和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIImageView介绍

UIImageView是用来放置图片的容器,在app的开发中,想要展示图片就要使用UIImageView这个控件。下面来介绍使用

UIImageView的创建

UIImageView和UILabel一样都是继承自UIView的,所以UIImageView的创建和UIView一样。

    //UIImageView的创建 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];

使用command+鼠标左键查看UIImageView的头文件可以看到除了继承自UIView的创建方法还有2个创建方法

    - (instancetype)initWithImage:(nullable UIImage *)image;
    - (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage NS_AVAILABLE_IOS(3_0);

无论是哪一种创建方法,有一点需要明确的是,控件是用来展示图片的。所以位置大小和图片是关键。下面来介绍一下UIImageView的常用属性和方法

UIImageView的常用属性

1、image属性
image属性是图片,即加载到UIImageView上的图片,通过这个属性的设置可以让图片展示到界面上。

    //给imageView设置图片  这里使用了UIImage类来给image属性赋值 UIImage类给image赋值常用的方法有两个
    //+ (nullable UIImage *)imageNamed:(NSString *)name;                使用图片名称赋值
    //+ (nullable UIImage *)imageWithContentsOfFile:(NSString *)path;   使用图片地址赋值
    imageView.image = [UIImage imageNamed:@"image"];

UIImageView其他的常用属性都是继承自UIView。这里不再重复介绍,在UIImageView的头文件中可以看到一些其他的属性和方法:

@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
@property (nullable, nonatomic, copy) NSArray<UIImage *> *highlightedAnimationImages NS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
@property (nonatomic) NSTimeInterval animationDuration;         // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
@property (nonatomic) NSInteger      animationRepeatCount;      // 0 means infinite (default is 0)

- (void)startAnimating;
- (void)stopAnimating;
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, getter=isAnimating) BOOL animating;
#else
- (BOOL)isAnimating;

这些是和图片有关的动画设置,这里先不记录,等以后写到关于动画的时候在统一记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值