UI学习 第四章 UIImage UIImageView 手势
UIImage
1.获取图片方法
第一种 UIImage
*img1 = [
UIImage
imageNamed
:
@"1.png"
];
第二种 UIImage
*img1 = [
UIImage
imageWithContentsOfFile
:
@"/Users/shangchengjiaoyu/Desktop/377adab44aed2e73d83b0e798701a18b86d6fa91.png
”
];//通过路径获取图片
第三种
NSData
*data = [
NSData
dataWithContentsOfURL
:[
NSURL
URLWithString
:
@"http://id.topit.me/d/4e/5e/1194980120f565e4edo.jpg"
]];
//
通过
url
拿到图片的数据
UIImage *img1 = [UIImage imageWithData:data];//通过url拿到图片
2.通过图片获取图片的数据
第一种 NSData
*data1 =
UIImageJPEGRepresentation
(img1,
0.5
);
;//图片转换成data,第一个是要转换的图片对象,第二个参数代表压缩质量(0~1浮点,1代表不压缩)
NSLog(@"%@",data1);
3.获取图片的宽高
NSLog
(
@"%f , %f"
,img1.
size
.
width
,img1.
size
.
height
);
第二种
NSData
*data1 =
UIImagePNGRepresentation
(img1);这种不需要压缩
4.UIImageView
UIImageView
*imgview = [[
UIImageView
alloc
]
initWithImage
:img1];
imgview.frame = CGRectMake(30, 30, 200, 200);
UIImageView
1.创建
UIImageView
*imageView = [[
UIImageView
alloc
]
initWithFrame
:
self
.
view
.
bounds
];
2.添加一张图片
imageView.
image
= [
UIImage
imageNamed
:
@"1.jpg"
];
3.
调整
imageview
,调整其尺寸适应图片大小(
imageview
的
size
等于图片的
size
)
[imageView
sizeToFit
];
4.
以
imageview
宽和高中较小的为标准进行等比缩放
imageView.
contentMode
=
UIViewContentModeScaleAspectFit
;
5.以
imageview
宽和高中较大的为标准进行等比缩放,如果
imageview
尺寸不够,则进行负方向调整,但是不会影响
iamgeview
的
frame
imageView.
contentMode
=
UIViewContentModeScaleAspectFill
;
6.
图片显示image的原始大小,显示的位置居于imageview的中心,如果imageview的尺寸不够,则负向调整
imageView.
contentMode
=
UIViewContentModeCenter
;
7.循环播放的图片
imgView
.
animationImages
= imgArr;
8.循环所有图片所需时间
imgView
.
animationDuration
=
3
;
9.循环次数,0代表无限循环
imgView
.
animationRepeatCount
=
0
;
10.开始/停止动画(注意开始动画要写在设置完成以后)
[
imgView
startAnimating
];
[
imgView
stopAnimating
];
11.允许交互
imgView
.
userInteractionEnabled
=
YES
;
手势
//
注意:同一个手势不能添加到多个视图上
1.将手势tap添加到
ImageView上
UIImageView
*v1 = [[
UIImageView
alloc
]
initWithImage
:[
UIImage
imageNamed
:
@"1"
]];
v1. frame = CGRectMake ( 80 , 80 , 200 , 200 );
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (test)];
v1. userInteractionEnabled = YES ;
v1. frame = CGRectMake ( 80 , 80 , 200 , 200 );
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (test)];
v1. userInteractionEnabled = YES ;
[v1 addGestureRecognizer:tap];
2.点击几次触发
tap.
numberOfTapsRequired
=
2
;
3.几个手指点击触发
tap.
numberOfTouchesRequired
=
2
;
4.判断哪个视图被点击
-(
void
)imageTaped:(
UITapGestureRecognizer
*)ges{
UIView *v = ges.view;//看ges这个手势加到了哪个视图上
NSLog ( @" 图片被点击 :%zi" ,v. tag );
}
4.拖动手势《必须要开用户交互》
(将手势pan添加到image1视图上)
UIPanGestureRecognizer
*pan = [[
UIPanGestureRecognizer
alloc
]
initWithTarget
:
self
action
:
@selector
(imageViewpan:)];
[imgView1 addGestureRecognizer:pan];
[self.view addSubview:imgView];
(显示状态)理解
-(
void
)test:(
UIPanGestureRecognizer
*)pan{
NSLog
(
@"----%@"
,
NSStringFromCGPoint
([pan
locationInView
:
self
.
view
]));//输出坐标
NSLog
(
@"
状态
%ld"
,(
long
)pan.
state
);
CGPoint
point = [pan
locationInView
:
self
.
view
];//状态1(开始
)状态2(正在拖动)状态3(停止)
pan.
view
.
center
= point;
}