zoukankan      html  css  js  c++  java
  • iOS开发之六:常用控件--UIImageView的使用

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

    1、常用的属性以及方法

    <span style="font-size:14px;">// 初始化图片
    - (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;</span>

    2、UIImageView的技术点

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

      

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

    方法一:给UIImageView加上手势

    示例代码如下:

    <span style="font-size:14px;">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];
    [tapGesture release];
    [self.view addSubView:imageView1];
    [imageView1 release];</span>
    上面代码是先创建了一个UIImageView,然后给其绑定了一个tap手势,通过实现手势的方法来处理点击事件。

    方法二:在UIImageView上添加一个自定义的透明的Button(或者干脆就用button来实现这个功能)

    <span style="font-size:14px;">UIImageView *imageView1 = [[UIImage alloc] initWithFrame:CGRectMake(110,20,100,100)];
    imageView1.userInteractionEnabled = YES;
    imageView1.backgroundColor = [UIColor clearColor];
    imageView1.image = [UIImage imageNamed:@"love.png"];
    UIButton *button = [[UIButton alloc] initWithFrame:imageView1.bounds];
    [button addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubView:button];
    [button release];
    [imageView1 release];</span>
    有时候我们需要对一个图片进行处理,也会有一个常用的方法:

    <span style="font-size:14px;">- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;</span>
    该方法第一个参数是拉伸距离原点的横向距离,第二个参数是纵向距离距离,会将一个图片拉伸处理后返回一个新的图片。

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

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

    <span style="font-size:14px;">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];
    [imageView1 release];</span>
    其实我们可以用第三方的框架来实现,这个框架叫SDWebImage,它对UIImageView添加了category方法,可以直接异步加载一个NSURL。

    <span style="font-size:14px;">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 ]];
    [imageView1 release];</span>
    最后,补充一点,UIImageView可以播放多张图片。代码如下:

    <span style="font-size:14px;">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];</span>
    上面的animationDuration是动画持续时间,就是两张图片一共的播放时间,如果是4张图片,就是每张播放播放0.5秒的样子。

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

  • 相关阅读:
    vs 2005 使用 UpdatePanel 配置
    gridview checkbox 列
    csv 格式文件 导入导出
    UML中数据流图,用例图,类图,对象图,角色图,活动图,序列图详细讲述保存供参考
    c# 根据经纬度 求两点之间的距离
    c# 加密汇总
    日期获取 第一天,最后一天
    求点到直线的垂足
    c# 修改注册表
    HDOJ_1548 上楼梯 DJ
  • 原文地址:https://www.cnblogs.com/wanghang/p/6298898.html
Copyright © 2011-2022 走看看