PhotoView.m
14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// PhotoView.m
// aoyouHH
//
// Created by jinzelu on 15/4/30.
// Copyright (c) 2015年 jinzelu. All rights reserved.
//
#import "PhotoView.h"
//#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
//#import "UIView+Toast.h"
//#import "UIAlertView+NTESBlock.h"
//#import "SVProgressHUD.h"
@interface PhotoView ()<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
MBProgressHUD *hud;
NSString *path;
}
@end
@implementation PhotoView
@synthesize moviePlayer = _moviePlayer;
@synthesize avPlayer = _avPlayer;
-(id)initWithFrame:(CGRect)frame withPhotoUrl:(NSString *)photoUrl{
self = [super initWithFrame:frame];
if (self) {
//添加scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_scrollView];
//添加图片
self.imageView = [[UIImageView alloc] initWithFrame:_scrollView.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager.imageCache containsImageForKey:photoUrl cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
BOOL isInCache = (containsCacheType == SDImageCacheTypeNone)?NO:YES;
if (!isInCache) {//没有缓存
self->hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
self->hud.mode = MBProgressHUDModeDeterminate;
}
[self.imageView sd_setImageWithURL:[NSURL URLWithString:photoUrl] placeholderImage:[UIImage imageNamed:@"comment_empty_img"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
self->hud.progress = ((float)receivedSize)/expectedSize;
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
NSLog(@"图片加载完成");
if (!isInCache) {
[self->hud hide:YES];
}
}];
}];
// [manager cachedImageExistsForURL:[NSURL URLWithString:photoUrl] completion:^(BOOL isInCache) {
// if (!isInCache) {//没有缓存
// hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
// hud.mode = MBProgressHUDModeDeterminate;
// }
// [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoUrl] placeholderImage:[UIImage imageNamed:@"comment_empty_img"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
// hud.progress = ((float)receivedSize)/expectedSize;
// } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
// NSLog(@"图片加载完成");
// if (!isInCache) {
// [hud hide:YES];
// }
// }];
//
// }];
[self.imageView setUserInteractionEnabled:YES];
[_scrollView addSubview:self.imageView];
//添加手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;//需要点两下
twoFingerTap.numberOfTouchesRequired = 2;//需要两个手指touch
longPressGesture.minimumPressDuration = 0.5; //(2秒)
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[self.imageView addGestureRecognizer:longPressGesture];
[singleTap requireGestureRecognizerToFail:doubleTap];//如果双击了,则不响应单击事件
[_scrollView setZoomScale:1];
}
return self;
}
-(id)initWithFrame:(CGRect)frame withPhotoImage:(UIImage *)image{
self = [super initWithFrame:frame];
if (self) {
//添加scrollView
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_scrollView];
//添加图片
self.imageView = [[UIImageView alloc] initWithFrame:_scrollView.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.imageView setImage:image];
[self.imageView setUserInteractionEnabled:YES];
[_scrollView addSubview:self.imageView];
//添加手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;//需要点两下
twoFingerTap.numberOfTouchesRequired = 2;//需要两个手指touch
longPressGesture.minimumPressDuration = 0.5; //(2秒)
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:longPressGesture];
[self.imageView addGestureRecognizer:twoFingerTap];
[singleTap requireGestureRecognizerToFail:doubleTap];//如果双击了,则不响应单击事件
[_scrollView setZoomScale:1];
}
return self;
}
#pragma mark - UIScrollViewDelegate
//scroll view处理缩放和平移手势,必须需要实现委托下面两个方法,另外 maximumZoomScale和minimumZoomScale两个属性要不一样
//1.返回要缩放的图片
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
//2.重新确定缩放完后的缩放倍数
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
}
#pragma mark - 图片的点击,touch事件
-(void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"单击");
if (gestureRecognizer.numberOfTapsRequired == 1) {
[self.delegate TapHiddenPhotoView];
}
}
-(void)handleDoubleTap:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(@"双击");
if (gestureRecognizer.numberOfTapsRequired == 2) {
if(_scrollView.zoomScale == 1){
float newScale = [_scrollView zoomScale] *2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}else{
float newScale = [_scrollView zoomScale]/2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}
}
}
-(void)handleTwoFingerTap:(UITapGestureRecognizer *)gestureRecongnizer{
NSLog(@"2手指操作");
float newScale = [_scrollView zoomScale]/2;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecongnizer locationInView:gestureRecongnizer.view]];
[_scrollView zoomToRect:zoomRect animated:YES];
}
//常摁手势触发方法
-(void)longPressGesture:(id)sender
{
UILongPressGestureRecognizer *longPress = sender;
if (longPress.state == UIGestureRecognizerStateBegan)
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *Photo = [UIAlertAction actionWithTitle:@"保存到手机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UIImageWriteToSavedPhotosAlbum([self.imageView image], nil, nil,nil);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"存储照片成功"
message:@"您已将照片存储于手机中,打开照片即可查看。"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}];
UIAlertAction *Cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:Photo];
[alertVC addAction:Cancel];
[self.delegate longPressGesturePhotoView:alertVC];
}
}
#pragma mark - 缩放大小获取方法
-(CGRect)zoomRectForScale:(CGFloat)scale withCenter:(CGPoint)center{
CGRect zoomRect;
//大小
zoomRect.size.height = [_scrollView frame].size.height/scale;
zoomRect.size.width = [_scrollView frame].size.width/scale;
//原点
zoomRect.origin.x = center.x - zoomRect.size.width/2;
zoomRect.origin.y = center.y - zoomRect.size.height/2;
return zoomRect;
}
- (MPMoviePlayerController*)moviePlayer{
if (!_moviePlayer) {
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
_moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
_moviePlayer.fullscreen = YES;
}
return _moviePlayer;
}
- (AVPlayerViewController*)avPlayer{
if (!_avPlayer) {
_avPlayer = [[AVPlayerViewController alloc]init];
_avPlayer.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
}
return _avPlayer;
}
- (void)startPlay{
self.avPlayer.view.frame = CGRectMake(0, 0, ScreenWidth, self.bounds.size.height);
[self addSubview:self.avPlayer.view];
UIImageView *img_back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xn talk-ios-37"]];
[self addSubview:img_back];
[img_back mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.avPlayer.view.mas_top).offset(40);
make.left.equalTo(self.mas_left).offset(20);
}];
UIButton *btn_back = [[UIButton alloc]init];
[btn_back addTarget:self action:@selector(removeReader) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn_back];
[btn_back mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(img_back.mas_centerY);
make.left.equalTo(self.mas_left).offset(20);
make.width.equalTo(@50);
make.height.equalTo(@50);
}];
}
- (void)removeReader{
[self.delegate TapHiddenPhotoView];
}
- (void)onTap: (UIGestureRecognizer *)recognizer
{
switch (self.moviePlayer.playbackState)
{
case MPMoviePlaybackStatePlaying:
[self.moviePlayer pause];
break;
case MPMoviePlaybackStatePaused:
case MPMoviePlaybackStateStopped:
case MPMoviePlaybackStateInterrupted:
[self.moviePlayer play];
break;
default:
break;
}
}
- (void)moviePlaybackComplete: (NSNotification *)aNotification
{
if (self.moviePlayer == aNotification.object)
{
NSDictionary *notificationUserInfo = [aNotification userInfo];
NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
MPMovieFinishReason reason = [resultValue intValue];
if (reason == MPMovieFinishReasonPlaybackError)
{
NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"];
NSString *errorTip = [NSString stringWithFormat:@"%@: %@",@"播放失败", [mediaPlayerError localizedDescription]];
[self makeToast:errorTip
duration:2
position:CSToastPositionCenter];
}
}
}
- (void)moviePlayStateChanged: (NSNotification *)aNotification
{
if (self.moviePlayer == aNotification.object)
{
switch (self.moviePlayer.playbackState)
{
case MPMoviePlaybackStatePlaying:
break;
case MPMoviePlaybackStatePaused:
case MPMoviePlaybackStateStopped:
case MPMoviePlaybackStateInterrupted:
case MPMoviePlaybackStateSeekingBackward:
case MPMoviePlaybackStateSeekingForward:
break;
}
}
}
//- (void)downLoadVideo:(void(^)(NSError *error))handler{
// [SVProgressHUD show];
// __weak typeof(self) wself = self;
// [[NIMSDK sharedSDK].resourceManager download:_video.url filepath:_video.path progress:^(float progress) {
// [SVProgressHUD showProgress:progress];
// } completion:^(NSError *error) {
// if (wself) {
// [SVProgressHUD dismiss];
// if (handler) {
// handler(error);
// }
// }
// }];
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end