美文网首页
iOS开发之六:常用控件--UIImageView的使用

iOS开发之六:常用控件--UIImageView的使用

作者: 咖啡绿茶1991 | 来源:发表于2018-05-03 17:45 被阅读0次

    UIImageView是我们做iOS开发用的非常多的一个控件,IOS中的各种图片,包括头像,有的背景图片等基本都要用到这个控件。

    1、常用的属性以及方法

    // 初始化图片  

    - (id)initWithImage:(UIImage *)image;  

    // 初始化带高亮的图片  

    - (id)initWithImage:(UIImage *)image highlightedImage:(UIImage  

    *)highlightedImage  

    // 点语法设置图片  

    @property(nonatomic,retain) UIImage *image;  

    // 点语法设置高亮图片  

    @property(nonatomic,retain) UIImage *highlightedImage  

    // 是否打开用户交互,默认为NO  

    @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  

    @property(nonatomic)    UIViewContentMode contentMode;

    2、UIImageView的技术点

    UIImageView常用属性就这么多,但是UIImageView的技术点确很多,比如contentMode,这个属性是继承自父类UIView的,但是这个枚举类型中三个类型是专门为UIImageView使用的,用来处理图片的拉伸方式。对应的拉伸效果如下:

    有的时候,我们需要给UIImageView加上点击事件,这时候我们有两种方式来实现这个功能。首先两种方式都需要设置userInteractionEnabled为YES,因为UIImageView的这个属性默认为NO,其实UILabel也是默认为NO的。

    方法一:给UIImageView加上手势

    示例代码如下:

    UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];  

    imageView1.userInteractionEnabled = YES;  

    imageView1.backgroundColor = [UIColor clearColor];  

    imageView1.image = [UIImage imageNamed:@"love.png"];  

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] intiWithTarget:self action:@selector(tapAction)];  

    [imageView1 addGestureRecognizer:tapGesture];  

    [self.view addSubView:imageView1]; 

    有时候我们需要对一个图片进行处理,也会有一个常用的方法:

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

    该方法第一个参数是拉伸距离原点的横向距离,第二个参数是纵向距离距离,会将一个图片拉伸处理后返回一个新的图片。

    还有一个技术点,有的时候,我们需要加载网络图片,不是app里的资源图片,这时候怎么处理呢?UIImageView是不可以加载网络图片的。

    我们可以利用UIImage的imageWithData这个方法,但是这个方法会阻塞主线程,示例代码如下:

    UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];  

    imageView1.userInteractionEnabled = YES;  

    imageView1.backgroundColor = [UIColor clearColor];  

    NSString *imagePath = @"http://www.baidu.com/xxxxx.png";  

    NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:imagePath]];  

    imageView1.image = [UIImage imageWithData:data];  

    其实我们可以用第三方的框架来实现,这个框架叫SDWebImage,它对UIImageView添加了category方法,可以直接异步加载一个NSURL。

    UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];  

    imageView1.userInteractionEnabled = YES;  

    imageView1.backgroundColor = [UIColor clearColor];  

    NSString *imagePath = @"http://www.baidu.com/xxxxx.png";  

    //加载网络图片数据  

    [_image setImageWithURL:[NSURL URLWithString:*imagePath ]];  

    最后,补充一点,UIImageView可以播放多张图片。代码如下:

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(160-41/2.0, 100, 100, 150)];  

    imgView.animationImages = @[[UIImage imageNamed:@"img_two.jpg"],[UIImage imageNamed:@"img_three.jpg"]];  

    imgView.animationDuration = 2;  

    [imgView startAnimating];

    上面的animationDuration是动画持续时间,就是两张图片一共的播放时间,如果是4张图片,就是每张播放播放0.5秒的样子。

    还有一个animationRepeatCount是设置重复次数,默认是0次,无限重复。

    版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u011619283/article/details/23945573

    相关文章

      网友评论

          本文标题:iOS开发之六:常用控件--UIImageView的使用

          本文链接:https://www.haomeiwen.com/subject/gfjlrftx.html