两种image初始化方法
第一种初始化方法:适用于反复用回缓存图片
UIImage * image1 =[UIImage imageNamed:@"baby.jpg"];
UIImageView * imageView1= [[UIImageView alloc] initWithImage:image1];
imageView1.frame = self.view.frame;
[self.view addSubview:imageView1];
第二种初始化方法:不会缓存适用于用一次就不用的。
//NSString * imagePath = [[NSBundle mainBundle]pathForResource:@"baby.jpg" ofType:nil];
//UIImage * image1 = [UIImageimageWithContentsOfFile:imagePath];
UIImageView * imageView1= [[UIImageView alloc] initWithImage:image1];
imageView1.frame = self.view.frame;
[self.view addSubview:imageView1];
//从网络加载图片
//NSString * urlString = @"http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg";
////获取二进制流
//NSData * data = [NSDatadataWithContentsOfURL:[NSURL URLWithString:urlString]];
//NSData转换UIImage
UIImage * image1= [UIImage imageWithData:data];
//创建imageView
UIImageView* imageView1 = [[UIImageView alloc] initWithImage:image1];
imageView1.frame =self.view.frame;
//设置图片,更改图片显示样式
UIImage * image =[UIImage imageNamed:@"baby.jpg"];
UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
imageview.backgroundColor = [UIColorgrayColor];
imageview.image = image;
//设置拉伸平铺等样式
//UIViewContentModeScaleToFill默认
//常用这种
imageview.contentMode = UIViewContentModeScaleAspectFill;
//根据控件的Bounds剪切超出的部分(切元的用,设置图片阴影的关闭)
imageview.clipsToBounds = YES;
contentMode属性
这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形,图片填充满frame(默认)。
UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。
//圆角半径
imageView.layer.cornerRadius =imageView.frame.size.width / 2;
//设置边框颜色
imageView.layer.borderColor = [UIColorblueColor].CGColor;
////设置边框宽度
imageView.layer.borderWidth = 2.f;
//设置阴影颜色
imageView.layer.shadowColor = [UIColoryellowColor].CGColor;
//设置阴影的大小和投影的位置
imageView.layer.shadowOffset = CGSizeMake(20, 20);
//设置阴影的透明度
imageView.layer.shadowOpacity = 0.3;
图片的拉伸
UIImage * image = [UIImageimageNamed:@"backgroundImage"];
//设置拉伸内边距(拉伸的是图形匡的内部没有拉伸四角四角是6像素)
//UIEdgeInsets insets = UIEdgeInsetsMake(6,6, 6, 6);
////设置拉伸图片
image = [imageresizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
UIImageView * imageView = [[UIImageViewalloc] initWithImage:image];
imageView.frame= CGRectMake(20, 50, CGRectGetWidth(self.view.frame)-40, 50);
另一种方式,点击图片编辑找到要拉伸的图片,点击右下角Show Slicing选择拉伸方式即可。
//制作动图动画
NSMutableArray * array =[NSMutableArray array];
for (int i = 0; i < 5; i++) {
NSString * imageName = [NSStringstringWithFormat:@"hehua0%d",i+1];
UIImage * image = [UIImageimageNamed:imageName];
[array addObject:image];
}
UIImageView * imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
self.imageView = imageView;
//设置序列帧图像数组
imageView.animationImages = array;
//设置动画时间
imageView.animationDuration = 1;
//设置播放次数0代表无限次
imageView.animationRepeatCount = 0;
[self.viewaddSubview:imageView];
//开始播放动画
[imageViewstartAnimating];
//停止动画
[imageView stopAnimating];
网友评论