iOS获取本地视频和网络URL视频的缩略图方法

首先大家先添加AVFoundation和CoreMedia.framework两个框架

NSString *path = @"www.51ios.net/本地路径"
MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
 UIImage *img = [51iosMPMovie thumbnailImageAtTime:0.0
timeOption:MPMovieTimeOptionNearestKeyFrame];

此处的img就是时间在0.0的缩略图
  • 第二种获取网络视频的缩略图
NSString *videoURL = @"http://www.51ios.net/archives/784"

MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; 51iosMPMovie.shouldAutoplay = NO;
UIImage *thumbnail = [51iosMPMovie thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];

此处的thumbnail就是网络视频的缩略图
  • 第三站方法用AVFoundation实现
+(UIImage *)getThumbnailImage:(NSString *)videoURL

{

 AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];

 AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];

gen.appliesPreferredTrackTransform = YES;

 CMTime time = CMTimeMakeWithSeconds(0.0, 600);

 NSError *error = nil;

 CMTime actualTime;

 CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

 UIImage *thumb = [[UIImage alloc] initWithCGImage:image];

 CGImageRelease(image);

 return thumb;
}


文/Lonely__(简书作者)
原文链接:http://www.jianshu.com/p/cc52403f7d42
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将网络音频URL存储到本地并播放,可以按照以下步骤进行操作: 1. 首先,使用AVPlayer或AVPlayerViewController来播放网络音频。例如: ```swift let url = URL(string: "http://example.com/audio.mp3") let player = AVPlayer(url: url!) let playerViewController = AVPlayerViewController() playerViewController.player = player present(playerViewController, animated: true) { player.play() } ``` 2. 接下来,使用URLSession来下载音频文件并将其保存到本地。例如: ```swift let url = URL(string: "http://example.com/audio.mp3") let task = URLSession.shared.downloadTask(with: url!) { (location, response, error) in guard let location = location else { return } let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let destinationUrl = documentsUrl.appendingPathComponent("audio.mp3") do { try FileManager.default.moveItem(at: location, to: destinationUrl) // 播放本地音频 let player = AVPlayer(url: destinationUrl) let playerViewController = AVPlayerViewController() playerViewController.player = player present(playerViewController, animated: true) { player.play() } } catch { print(error) } } task.resume() ``` 3. 最后,为了节省空间,可以在不需要时删除本地音频文件。例如: ```swift let fileManager = FileManager.default let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! let destinationUrl = documentsUrl.appendingPathComponent("audio.mp3") do { try fileManager.removeItem(at: destinationUrl) } catch { print(error) } ``` 以上就是实现将网络音频URL存储到本地并播放的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值