UIImage,UIImageView的使用

UIImage,UIImageView的使用

上节回顾

上一节中我们总结了UIView的基本属性与基本方法以及UIView的继承与被继承关系,我们还介绍了UIViewController的使用以及他与UIView之间的关系。最后我们总结了OC中的继承以及各控件之间的继承关系。

本节内容

  • UIImage是什么?
  • UIImageView是什么?
  • UIImage与UIImageView之间有什么关系?
  • UIImageView与UIView之间的关系。

UIImage是什么

在开发者文档中UIImage的定义如下

An object that manages image data in your app.

意思就是用于管理图片的一个类。也就是说在App中我们要使用一个图片首先就要对其进行实例化,实例化对应的类就是UIImage。

UIImage *img = [UIImage imageNamed:@"rjyfzx"];

这是实例化的一种方法,在引用之前我们需要把图片导入到我们的项目中。
另一种方法是引用图片路径进行加载图片,这种方法对于只用一次的图片来说能减少内存,提升应用速度。

   UIImage *img = [[UIImage alloc]initWithContentsOfFile:@"图片路径"];

UIImage属性及方法

  • size
    设置图片尺寸
@property(nonatomic,readonly) CGSize size;
  • scale
    图片的缩放比例
@property(nonatomic,readonly) CGFloat scale
  • (方法)加载图片(分清实例方法与类方法)
+ (nullable UIImage *)imageNamed:(NSString *)name; 
+ (nullable UIImage *)imageNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection
+ (nullable UIImage *)imageWithContentsOfFile:(NSString *)path;
- (nullable instancetype)initWithContentsOfFile:(NSString *)path;
  • 从0或1开始读取带有后缀的文件序列。创建并返回一个动画图像。
+ (nullable UIImage *)animatedImageNamed: (NSString *)name duration: (NSTimeInterval)duration;

绘图方法

  • 在 Point这个点绘图。
- (void)drawAtPoint: (CGPoint)point;
  • 在某个具体位置进行绘图.
- (void)drawInRect: (CGRect)rect;

以上就是UIImage的一些常用属性和方法。

UIImageView是什么

在开发者文档中UIImageView的定义如下

An object that displays a single image or a sequence of animated images in your interface.

它的作用就是用来展示我们创建的UIImage。它是UIView的子类。其实构造方法如下:

UIImageView  *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"图片名称"];

常用属性即方法

  • image。图片属性默认为空。
@property (nullable, nonatomic, strong) UIImage *image; // default is nil
  • highlightedImage。高亮状态图片
@property (nullable, nonatomic, strong) UIImage *highlightedImage
  • userInteractionEnabled用户交互功能
@property (nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
  • animationImages 动图图片。动图图片需要拆分为一张一张的形式再进行播放。
@property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages;
  • animationDuration。动图播放时间。
@property (nonatomic) NSTimeInterval animationDuration; 
  • animationRepeatCount 动画重复次数
@property (nonatomic) NSInteger      animationRepeatCount; 

当然子类可以继承父类的属性,所以UIView的backgroundColor,alpha,frame等属性,UIImageView仍拥有。在这里不再赘述。

  • - (void)startAnimating;开始播放动画
    
- (void)startAnimating;
  • - (void)stopAnimating;停止播放
    
- (void)stopAnimating;

如上所述,UIImageView可以继承UIView的方法,其他方法也不再进行赘述。
接下来就看下UIImageView展示图片和播放动图的效果。
动态图片和静态图片展示
代码如下:

//这里动图的名字只需把根名写出即可,后面的数字可以不用加
    UIImage *img = [UIImage animatedImageNamed:@"l" duration:4];
    UIImage *img1 = [UIImage imageNamed:@"20151020185449140"];
    UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100,300, 300, 450)];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 350)];
    imgView1.image = img1;
    imgView.image = img;
    [self.view addSubview:imgView1];
    [self.view addSubview:imgView];

UIImage与UIImageView之间的关系

通过上面的描述,UIImageView是用来展示UIImage的,相当于一个相框。UIImage就是图片。两者属于不同的类,各有自己的构造方法和属性。在使用中注意区分。

UIImageView与UIView有什么关系

UIImageView是UIView的子类,UIImageView继承UIView的属性和方法,当然也可以重写父类中的方法。

总结

这篇主要讲了UIImageView与UIImage的属性和方法,在今后的使用中UIImageView是我们最经常使用的一个控件,许多应用中的文字看起来和普通文字差不多,但是为了美观,一般都是用图片(图片中包含所需文字)来代替文字。当然其他属性可以通过开发者文档来了解。

下节预告

UITableView,UITableViewController的介绍与使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值