美文网首页
IM聊天图片视频浏览器 跟微信一样

IM聊天图片视频浏览器 跟微信一样

作者: 老皮虾 | 来源:发表于2017-02-04 14:44 被阅读84次

思路:底层控件可选scrollview和collectionView,视频播放器选用AVPlayer功能齐全。整个控制器封装在一个controller里,数据源开辟外部接口即可;

1、新建一个ZTHPhotoBrower的控制器,声明它的代理和实例化collectionView和AVPlayer;

2、_collectionView.contentOffset = CGPointMake(self.index * (IPHONE_WIDTH + CMBrowseSpace), 0);// 设置每个cell之间的间距

3、// 播放完成的通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidPlayToEndTime) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

4、#pragma mark - 第一次选中滚动到具体的位置

-(void)browerScrollViewToIndex:(NSInteger)index{

self.unChangeIndex = self.index;

NSIndexPath *indexpath = [NSIndexPath indexPathForRow:[@(self.index) unsignedIntegerValue] inSection:0];

[self.collectionView scrollToItemAtIndexPath:indexpath

atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally

animated:NO];

}

- (void)currentIndex:(NSNumber *)index {

kWeakSelf

dispatch_async(dispatch_get_main_queue(), ^{

kStrongSelf

NSIndexPath *indexpath = [NSIndexPath indexPathForRow:[index unsignedIntegerValue] inSection:0];

[self.collectionView scrollToItemAtIndexPath:indexpath

atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally

animated:NO];

self.reloadNewIndexPath = nil;

dispatch_async(dispatch_get_main_queue(), ^{

self.isgetMoreModel = YES;

});

});

}

5、设置collectionViewCell的代理,cell上的任何操作都回调回控制器

6、collectionView视频和图片切换时,关闭视频/开启视频

#pragma mark - scrollViewDelegete

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGPoint pInView = [self.view convertPoint:self.collectionView.center toView:self.collectionView];

// 获取这一点的indexPath

NSIndexPath *indexPathNow = [self.collectionView indexPathForItemAtPoint:pInView];

self.numaberLabel.text = [NSString stringWithFormat:@"%ld/%ld",indexPathNow.row+1,[self.delegete photoVideoCellCoutNumber]];

// 获取当前的偏移量,计算当前第几页

if (self.unChangeIndex == indexPathNow.row) return;

self.unChangeIndex = (int)indexPathNow.row;

// 假如是图片则跳出

//    if (self.player) {

//        [self.player pause];

//    }

ZTHPhotoVideo *modelInfo = [self.delegete photoVideo:self photoVideoInformation:indexPathNow.row];

// 假如是图片移除播放

if (modelInfo.ZTHPhotoVideoType == ZTHPhotoVideoTypePhoto && self.playerLayer) {

[_player pause];

[self.playerLayer removeFromSuperlayer];

}

if (modelInfo.ZTHPhotoVideoType == ZTHPhotoVideoTypePhoto) return;

ZTHIMBrowerTableViewCell *cell = (ZTHIMBrowerTableViewCell *)[self.collectionView cellForItemAtIndexPath:indexPathNow];

// cell 为空的时候

if (!cell) return;

if (modelInfo.browerType == BrowerTypeIM) {

[cell isVideoBeginPlayey];

}else if (modelInfo.browerType == BrowerTypeCache){

[cell uploadVideoCacheLogin];

}

}

7、collectionViewCell的配置实例化一个scrollview作为图片拉伸旋转的底部控件、两种手势的添加、

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {

if (_modelInfo.ZTHPhotoVideoType == ZTHPhotoVideoTypeVideo) return;

CGFloat offsetX = (scrollView.tz_width > scrollView.contentSize.width) ? (scrollView.tz_width - scrollView.contentSize.width) * 0.5 : 0.0;

CGFloat offsetY = (scrollView.tz_height > scrollView.contentSize.height) ? (scrollView.tz_height - scrollView.contentSize.height) * 0.5 : 0.0;

self.imageViews.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);

}

8、图片展示的宽高比例

- (void)addjustFrame:(BOOL)isFirst srcImageView:(UIView *)srcImageView

{

if (self.imageViews.image == nil) {

return;

}

// 基本尺寸参数

CGSize boundsSize = [UIScreen mainScreen].bounds.size;

CGFloat boundsWidth = boundsSize.width;

CGFloat boundsHeight = boundsSize.height;

CGSize imageSize = self.imageViews.image.size;

CGFloat imageWidth = imageSize.width;

CGFloat imageHeight = imageSize.height;

// 设置伸缩比例

CGFloat minScale = boundsWidth / imageWidth;

if (minScale > 1) {

minScale = 1.0;

}

CGFloat maxScale = 2.0;

//    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {

//        maxScale = maxScale / [[UIScreen mainScreen] scale];

//    }

self.bottomScrollView.maximumZoomScale = maxScale;

self.bottomScrollView.minimumZoomScale = 1.0;

self.bottomScrollView.zoomScale = 1.0;

CGRect imageFrame = CGRectMake(0, 0, boundsWidth, imageHeight * boundsWidth / imageWidth);

// 内容尺寸

self.bottomScrollView.contentSize = CGSizeMake(0, imageFrame.size.height);

// y值

if (imageFrame.size.height < boundsHeight) {

imageFrame.origin.y = floorf((boundsHeight - imageFrame.size.height) / 2.0);

} else {

imageFrame.origin.y = 0;

}

// 第一次登陆

if (isFirst) {

UIView *locationView = [self.delegete cellLocationViewMessageID:_modelInfo.imgeId IMBrowerTableViewCell:self];

if (locationView) {

_imageViews.frame = [locationView convertRect:locationView.bounds toView:nil];

}

kWeakSelf

[UIView animateWithDuration:0.3 animations:^{

kStrongSelf

[self.delegete setCollectionBgroundColor:[UIColor blackColor] IMBrowerTableViewCell:self];

self.imageViews.frame = imageFrame;

[self performSelector:@selector(browerStatusBarHidden) withObject:nil afterDelay:0.2];

}];

}else{

self.imageViews.frame = imageFrame;

}

}

9、视频或者图片下载成功的回调刷新UI

#pragma mark - 清除缓存为入口的展示

- (void)uploadImageFromaCacheisFirstSigin:(BOOL)isFirst srcImageView:(UIView *)srcImageView

{

if (_modelInfo.ZTHPhotoVideoType == ZTHPhotoVideoTypeVideo) {

[self uploadVideoFromaCacheisFirstSigin:isFirst srcImageView:srcImageView];

return;

}

[[ZTHMessageResourceCenter share] queryImage:_modelInfo.imageVideoUrl complete:^(id resources, NSError *error) {

if (resources) {

self.imageViews.image = resources;

[self addjustFrame:isFirst srcImageView:srcImageView];

}

}];

}

// 类型为视频

- (void)uploadVideoFromaCacheisFirstSigin:(BOOL)isFirst srcImageView:(UIView *)srcImageView

{

// 缩略图

// 优先展示缩略图

[[ZTHMessageResourceCenter share] queryImage:_modelInfo.thumbUrl complete:^(id resources, NSError *error) {

if (!resources) {

NSLog(@"视频缩略图丢失*********************");

self.imageViews.image = [UIImage imageNamed:@"chat_icon_pic_overdue"];

return ;

}

self.imageViews.image = resources;

[self addjustFrame:isFirst srcImageView:srcImageView];

}];

}

// 播放视频 入口为缓存

-(void)uploadVideoCacheLogin

{

kWeakSelf

[[ZTHMessageResourceCenter share] fetchResourceWithKey:_modelInfo.imageVideoUrl complete:^(id resources, NSError *error) {

kStrongSelf

ZTHResource * tmpRes=resources;

if (tmpRes.localpath.length>0) {

if (self.delegete && [self.delegete respondsToSelector:@selector(playerVideoFromDB:playView:IMBrowerTableViewCell:)]) {

[self.delegete playerVideoFromDB:tmpRes.localpath playView:self.imageViews IMBrowerTableViewCell:self];

}

}

}];

}

相关文章

网友评论

      本文标题:IM聊天图片视频浏览器 跟微信一样

      本文链接:https://www.haomeiwen.com/subject/zjgjittx.html