【推荐理由】赠运费险
【优惠券】20 【券后价】59.00
【下单链接】18¥ CZ0001 G5fHdEqOsnB¥
这篇文章主要介绍一下UIImageView控件的各个属性及用法,本章还是有拾图网温州天时网站建设提供,简单的来时UIImageView控件,就是一个用来存放图片的容器,继承自View类,通常使用UIImage来加载图片,然后在吧UIimage变量赋值给UIImageView控件。经常性要用到,需要注意的:
1、创建控件;
2、frame与bounds属性;
3、contentMode属性;
4、更改位置的方法;
5、旋转图像操作;
6、缩放图片操作;
7、播放一系列图片的方法;
8、给UIIMageView控件添加 触发事件;
9、其他一些设置;
一、创建UIImageView控件:
UIImageView在显示图片时,首先把图片加载到UIImage中,然后通过其他方式使用该UIImage。以下说明了四种常用的加载UIImage的方法:
imageNamed:使用应用程序束中的一个文件来创建,IOS4以后的版本中可以省略图片扩展名;
imageWithCGImage:使用Quartz 2D对象创建UIImage,与initWithCGImage等效;
imageWithContentsOfFile:根据指定的路径创建UIImage,与initWithContentOfFile等效;
imageWithData:使用NSData创建,与initWithData等效;
另外,为支持iphone4之后的Retina显示屏,系统根据文件名后缀来加载最匹配的图像。
举个例子:
现在有两个包含相同图标的文件,一个是标准性的图片,一个是用于Retina屏幕显示的图片,标准命名为icon.png,而HD版本命名为icon@2x.png,加载期间,UIImage会自动去匹配需要加载的图像,如果没有HD版本的图像
创建UIImageView控件的有5种方法:
1、UIImageView *imageView1 = [[UIImageView alloc] init]; //创建时就设定位置和大小 2、UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)]; //通过UIImage类来实现 3、UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)]; //ImageView的highlighted属性是YES,即控件被选中时显示highlightedImage;默认情况下,显示initWithImage 4、UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)]; 5、UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];
在大多是数情况下,1,2,3这三种使用的比较多,第4中高亮图片切换时使用.
二、frame与bounds属性:
UIImageView控件可以frame或bounds来修改图片大小,那么这两个属性有什么不同之处呢?
frame:是设置控件的位置和大小;
bounds:只能设置大小,不能设置位置,其参数中的x、y不起作用即便是之前没有设定frame属性,控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。
所以通过这两个属性,可以修改通过“[[UIImageView alloc] initWithFrame:(CGRect)]”创建的,大小确定了的UIImageView控件。
修改案例:
1、如果要改变位置和大小——frame:
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth); imageView.frame = CGRectMake(0, 0, 320, 460);
2、只改变大小——bounds:
//在UIImageView创建成功后,再通过bounds修改时x、y将不会起作用 imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth); imageView.bounds = CGRectMake(100, 100, 160, 230);
三、显示方式设置——contentMode属性:
通过属性可以设置控件的显示方式,比如居中、居右,是否缩放等,有以下几个常量可供设定:
//根据视图的比例去拉伸图片内容。 UIViewContentModeScaleToFill //保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白 UIViewContentModeScaleAspectFit //保证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来 UIViewContentModeScaleAspectFill //凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中 //当单视图的尺寸位置发生变化的时候通过调用setNeedsDisplay方法来重新显示 UIViewContentModeRedraw //保持图片原比例在视图中间显示图片内容 //如果视图大小小于图片的尺寸,则图片会超出视图边界,下面类同 UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
上图分别对应:UIViewContentModeScaleToFill、UIViewContentModeScaleAspectFit、UIViewContentModeScaleAspectFill
四、改为位置:
如果需要修改控件的位置,有3种方法可以实现:
1、frame直接修改
imageView.frame = CGRectMake(0, 0, 320, 460);
2、center修改
imageView.center = CGPointMake(CGFloat x, CGFloat y);
3、transform属性
//其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。 imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
五、旋转效果设置:
//参数angle的单位是弧度 imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
注意:图片是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。
参数:
CGFloat angle
弧度,区别于度数,可以写一个宏定义:
#define degreesToRadians(x) (M_PI*(x)/180.0)
以上代码用于将度数转化成弧度。下图是旋转45度的情况:
六、图像的缩放:
还是通过使用transform属性设置:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
参数:
CGFloat scale_w与CGFloat scale_h分别表示将原来的宽度和高度缩放到多少倍,
下图是缩放到原来的0.6倍的示意图:
七、播放一系列图片
imageView.animationImages = imagesArray; // 设定所有的图片在多少秒内播放完毕 imageView.animationDuration = [imagesArray count]; // 不重复播放多少遍,0表示无数遍 imageView.animationRepeatCount = 0; // 开始播放 [imageView startAnimating];
说明:imagesArray是一些列图片的数组。如下图:
八、为图片添加单击事件:一定要先将userInteractionEnabled置为YES,这样才能响应单击事件。
imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap];
九、其他一些设置
imageView.hidden = YES或者NO; // 隐藏或者显示图片 imageView.alpha = (CGFloat) al; // 设置透明度 imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置高亮时显示的图片 imageView.image = (UIImage *)image; // 设置正常显示的图片 [imageView sizeToFit]; // 将图片尺寸调整为与内容图片相同
下面通过实用案例加深理解:
//下面是定义ImageView的方法
-(void) initImageView{ NSString * urlWeb=@"http://www.tshinet.com/themes/simplebootx/Public/images/logo.png"; UIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 200, 400)]; imageView.backgroundColor=[UIColor blueColor]; //两种方法,用imageNamed可以把图片放入内存,重复使用。但是太多会挂掉,一般重复使用的图片用imageNamed [imageView setImage:[self getImageFromURL:urlWeb]]; //[imageView setImage:[UIImage imageNamed:@"desc_bn.png"]]; [self.view addSubview:imageView]; }
//下载图片 -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIImage * result; NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; result = [UIImage imageWithData:data]; //下面是一句话搞定,上面是分开来做得。 //UIImage * result = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]]; return result; }
最后需要在view视图中的viewDidLoad函数里面通过调用initImageView方法来实现!感谢大家分享!