UIImageView

本文深入探讨了UIImageView在iOS开发中的使用方法,包括图片加载、圆角、边框、内容模式、透明度、动画及单击事件处理。通过具体代码示例,详细解释了每个属性的作用和应用场景。

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

// ----------------------------------------------UIImageView----------------------------------------------

    UIImageView *imageView = [[UIImageView alloc] init];

    imageView.image = [UIImage imageNamed:@"paper_list_homewok"];

    imageView.backgroundColor = [UIColor redColor];

    imageView.frame = CGRectMake(100, 100, 300, 300);

    

    // 加载图片方式

    //第二种:

    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpeg"];

    UIImage *images=[UIImage imageWithContentsOfFile:filePath];

    //[imageView setImage:images];

    //第三种:

    NSData *data=[NSData dataWithContentsOfFile:filePath];

    UIImage *image2=[UIImage imageWithData:data];

    [imageView setImage:image2];

    

    // 圆角

    imageView.layer.masksToBounds = YES;

    imageView.layer.cornerRadius = 10.0;

    

    // 边框

    imageView.layer.borderColor = [UIColor orangeColor].CGColor;

    imageView.layer.borderWidth = 2;

    

    //    (4)contentMode属性:当图片小于imageView的大小处理图片显示

    imageView.contentMode = UIViewContentModeCenter;

    

    // 播放多个图片

//    UIImage *image1 = [UIImage imageNamed:@"paper_list_homewok"];

//    UIImage *image2 = [UIImage imageNamed:@"question_check_selected"];

//    UIImage *image3 = [UIImage imageNamed:@"paper_list_homewok"];

//    UIImage *image4 = [UIImage imageNamed:@"question_check_selected"];

//    NSArray *imagesArray = @[image1,image2,image3,image4];

//    imageView.animationImages = imagesArray;

//    imageView.animationDuration = [imagesArray count];

//    [imageView startAnimating];

    

    //为图片添加单击事件:一定要先将userInteractionEnabled置为YES,这样才能响应单击事件

    imageView.userInteractionEnabled = YES;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];

    [imageView addGestureRecognizer:singleTap];

    

    // 透明度

    imageView.alpha = 0.5;

    

    imageView.highlightedImage = [UIImage imageNamed:@"question_check_selected"];

    

    // 设置图片的中心点

    imageView.center = CGPointMake(0,0);

 

转载于:https://www.cnblogs.com/xiangjune/p/4922573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值