提交 507e6bfd 编写于 作者: T toss156

添加沙盒浏览器音视频文件预览支持

上级 f9a87d20
......@@ -5,6 +5,8 @@
// Created by yixiang on 2018/6/20.
//
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "DoraemonSanboxDetailViewController.h"
#import "DoraemonToastUtil.h"
#import "UIView+Doraemon.h"
......@@ -14,6 +16,7 @@
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) AVPlayerViewController *playerView;
@end
......@@ -23,25 +26,35 @@
[super viewDidLoad];
self.title = DoraemonLocalizedString(@"文件预览");
if (self.filePath.length>0){
if (self.filePath.length>0) {
NSString *path = self.filePath;
if ( [path hasSuffix:@".png"] || [path hasSuffix:@".PNG"]
if ([path hasSuffix:@".png"] || [path hasSuffix:@".PNG"]
|| [path hasSuffix:@".jpg"] || [path hasSuffix:@".JPG"]
|| [path hasSuffix:@".jpeg"] || [path hasSuffix:@".JPEG"]
|| [path hasSuffix:@".gif"] || [path hasSuffix:@".GIF"] ){
|| [path hasSuffix:@".gif"] || [path hasSuffix:@".GIF"]) {
// 图片文件
UIImage *img = [[UIImage alloc]initWithContentsOfFile:self.filePath];
[self setOriginalImage:img];
}
else if ( [path hasSuffix:@".strings"] || [path hasSuffix:@".plist"] || [path hasSuffix:@".txt"] || [path hasSuffix:@".log"] || [path hasSuffix:@".csv"] ){
} else if ([path hasSuffix:@".strings"] || [path hasSuffix:@".plist"]
|| [path hasSuffix:@".txt"] || [path hasSuffix:@".log"]
|| [path hasSuffix:@".csv"]) {
// 文本文件
if ( [path hasSuffix:@".plist"] || [path hasSuffix:@".strings"] ){
[self setContent:[[NSDictionary dictionaryWithContentsOfFile:path] description]];
}
else{
} else {
NSData * data = [NSData dataWithContentsOfFile:path];
[self setContent:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}
}
else{
} else if ([path hasSuffix:@".mp4"] || [path hasSuffix:@".MP4"]
|| [path hasSuffix:@".mov"] || [path hasSuffix:@".MOV"]
|| [path hasSuffix:@".mp3"] || [path hasSuffix:@".MP3"]) {
// 音视频文件
[self setMediaFile:self.filePath];
} else {
// 其他文件
NSString *str = [NSString stringWithFormat:@"Not support %@ file!", [path pathExtension]];
[self setContent:str];
}
......@@ -50,7 +63,7 @@
}
}
- (void)setContent:(NSString *)text{
- (void)setContent:(NSString *)text {
_textView = [[UITextView alloc] initWithFrame:self.view.bounds];
_textView.font = [UIFont systemFontOfSize:12.0f];
_textView.textColor = [UIColor blackColor];
......@@ -65,7 +78,7 @@
[self.view addSubview:_textView];
}
- (void)setOriginalImage:(UIImage *)originalImage{
- (void)setOriginalImage:(UIImage *)originalImage {
CGFloat viewWidth = self.view.doraemon_width;
CGFloat viewHeight = self.view.doraemon_height;
CGFloat imageWidth = originalImage.size.width;
......@@ -80,7 +93,7 @@
scaledImageWidth = imageWidth / imageScale;
x = (viewWidth - scaledImageWidth) / 2;
y = 0;
}else{//图片横屏分量比较大
} else {//图片横屏分量比较大
imageScale = imageWidth / viewWidth;
scaledImageWidth = viewWidth;
scaledImageHeight = imageHeight / imageScale;
......@@ -93,5 +106,21 @@
[self.view addSubview:_imageView];
}
- (void)setMediaFile:(NSString *)mediaFile {
_playerView = [[AVPlayerViewController alloc] init];
NSURL *sourceMediaURL = [NSURL fileURLWithPath:mediaFile];
AVAsset *mediaAsset = [AVURLAsset URLAssetWithURL:sourceMediaURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:mediaAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
self.playerView.player = player;
self.playerView.view.translatesAutoresizingMaskIntoConstraints = YES;
self.playerView.showsPlaybackControls = YES;
self.playerView.view.bounds = self.view.bounds;
[self.playerView.player play];
[self addChildViewController:self.playerView];
[self.view addSubview:self.playerView.view];
}
@end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册